Mahdi Younesi
Mahdi Younesi

Reputation: 7509

Wrong table when updating "Belongs To" relationships in Laravel

Here I'm trying to save article's category with given id which exists in categories table,I have set the relationships but when trying to save, Laravel tries to insert new row in articles table not in the pivot table . Here is the error:

  *Unknown column 'category_id' in 'field list' (SQL: update `articles` set    `category_id` = 1, `updated_at` = 2015-11-16 13:15:32 where `id` = 53)* 

And these are relations and pivot table

class Article extends Model implements SluggableInterface
{ 
   public function category()
   {
     return $this->belongsTo('App\Category');
   }
}

class Category extends Model implements SluggableInterface
{
     public function articles()
     {
       return $this->hasMany('App\Article','article_category');
     }
}

      //pivot table
    Schema::create('article_category',function(Blueprint $table){
        $table->integer('article_id')->unsigned()->index();
        $table->foreign('article_id')->references('id')->on('articles')->onDelete('cascade');

        $table->integer('category_id')->unsigned()->index();
        $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');

        $table->timestamps();
    });

And here is my save function

public function store(ArticleRequest $request)
{
    $article=Auth::user()->articles()->create($request ->all());
    $category =Category::find($request ->input('category'));
    $article->category()->associate($category)->save();
}

Upvotes: 3

Views: 1500

Answers (2)

Saadettin Sivaz
Saadettin Sivaz

Reputation: 112

Your relationship type is many to many relotionship, not one to many.

Your models should be like this:

class Article extends Model implements SluggableInterface
{ 
   public function categories()
   {
     return $this->belongsToMany('App\Category', 'article_category');
   }
}

class Category extends Model implements SluggableInterface
{
     public function articles()
     {
       return $this->belongsToMany('App\Article', 'article_category');
     }
}

If you want one to many relationship not need 'article_category' table your migrations should like this:

Schema::create('articles',function(Blueprint $table){
    $table->integer('id')->increments();
    $table->string('title');
    $table->text('content');

    $table->integer('category_id')->unsigned()->index();
    $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');

    $table->timestamps();
});

and models:

class Article extends Model implements SluggableInterface
{ 
   public function category()
   {
     return $this->belongsTo('App\Category');
   }
}

class Category extends Model implements SluggableInterface
{
     public function articles()
     {
       return $this->hasMany('App\Article');
     }
}

Upvotes: 2

Calin Blaga
Calin Blaga

Reputation: 1373

Not sure but it looks to me that the relations should be:

articlass Article extends Model implements SluggableInterface
{ 
   public function category()
   {
     return $this->belongsTo('App\ArticleCategory', 'category_id');
   }
}

and

class Category extends Model implements SluggableInterface
{
     public function articles()
     {
        return $this->hasMany('App\ArticleCategory','article_id');
     }
}

so it references to the pivot table

Upvotes: 0

Related Questions