I have two table
- articles
- categories
I have a ArticleController. I want to edit the form. But it get errors.
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'category' in 'field list' (SQL: update
articlessetupdated_at= 2017-12-21 11:50:12,category= 1 whereid= 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'));
}
This error occurred on the following line.
$article->update($inputs);
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>
Category
class Category extends Model
{
protected $fillable = ['name', 'slug'];
public function articles()
{
return $this->belongsToMany(Article::class);
}
}
Article
public function categories()
{
return $this->belongsToMany(Category::class);
}