我有两张桌子
我有一个ArticleController。我想编辑表单。但它会出错。
SQLSTATE [42S22]:未找到列:1054'字段列表'中的未知列'类别'(SQL:update
articles
setupdated_at
= 2017-12-21 11:50:12,category
= 1其中id
= 1)
ArticleController.php
public function update(ArticleRequest $request, Article $article)
{
$file = $request->file('images');
$inputs = $request->all();
$article->categories()->sync(request('category'));
if($file) {
$inputs['images'] = $this->uploadImages($request->file('images'));
} else {
$inputs['images'] = $article->images;
$inputs['images']['thumb'] = $inputs['imagesThumb'];
}
unset($inputs['imagesThumb']);
$article->update($inputs);
return redirect(route('articles.index'));
}
此错误发生在以下行。
$物品─>更新($输入);
edit.blade.php
<select name="category[]" class="form-control" id="category" title=" Select your a categories..." multiple>
@foreach( $categories as $id => $name )
<option value="{{ $id }}" {{ in_array($id , $article->categories()->pluck('id')->toArray()) ? 'selected' : '' }}>{{ $name }}</option>
@endforeach
</select>
类别
class Category extends Model
{
protected $fillable = ['name', 'slug'];
public function articles()
{
return $this->belongsToMany(Article::class);
}
}
文章
public function categories()
{
return $this->belongsToMany(Category::class);
}
您的文章表没有名为category的列。你能发布你的文章表迁移吗?
这意味着你的category
表中没有articles
列。因此,请确保您拥有一个或使用其他列名称。
此外,您从输入中获得多个类别,因此使用此输入更新文章似乎不正确。
注意这一点
<select name="category[]"
也许你的意思
<select name="category_id[]"
这是因为您的文章模型上没有$ fillable数组,因此请求中的其他值正在泄漏到批量分配。