0
votes

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 articles set updated_at = 2017-12-21 11:50:12, category = 1 where 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'));
    }

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);
    }
4
please share your model class for "Category.php" and "Article.php" - xmhafiz
yes i have two model Category and Article. - user9085794

4 Answers

1
votes

It means that you don't have category column in your articles table. So make sure you have one or use other column name.

Also from input you are getting multiple categories, so it seems incorrect to update article with this input.

0
votes

Your articles table doesn't have a column called category. Can you post your article table migration please

0
votes

Pay attention to this

<select name="category[]" 

Perhaps you meant

<select name="category_id[]" 
0
votes

Its because you don't have a $fillable array on your Article model so other values from the request are leaking through to mass assignment.