【问题标题】:Wrong table when updating "Belongs To" relationships in Laravel在 Laravel 中更新“属于”关系时出现错误的表
【发布时间】:2015-11-16 09:49:31
【问题描述】:

在这里,我正在尝试使用类别表中存在的给定 id 保存文章的类别,我已经设置了关系,但是在尝试保存时,Laravel 尝试在 articles 表中插入新行而不是数据透视表。 这是错误:

  *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)* 

这些是关系和数据透视表

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();
    });

这是我的保存功能

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

【问题讨论】:

    标签: php mysql laravel pivot-table laravel-5.1


    【解决方案1】:

    你的关系类型是多对多关系,而不是一对多。

    你的模型应该是这样的:

    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');
         }
    }
    

    如果您希望一对多关系不需要“article_category”表,您的迁移应该是这样的:

    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();
    });
    

    和模型:

    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');
         }
    }
    

    【讨论】:

    • 一篇文章只属于一个分类,一个分类可以有很多篇文章,我的逻辑错了吗?
    • 第二个参数是一对多关系中的外键。但是多对多关系需要在第二个参数中使用数据透视表名称
    • 谢谢,我明白了,所以我删除了评论,我会在测试后投票。
    【解决方案2】:

    不确定,但在我看来,关系应该是:

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

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

    所以它引用了数据透视表

    【讨论】:

    • Calin Blaga,据我所知,第二个参数是数据透视表的名称,但我没有这样的表,我只有一个数据透视表来处理这些关系,那就是“article_category”
    • 外键:public function hasMany($related, $foreignKey = null, $localKey = null)
    猜你喜欢
    • 2023-03-28
    • 2021-06-20
    • 1970-01-01
    • 2016-07-25
    • 2019-09-22
    • 2018-06-02
    • 2018-05-03
    • 2021-03-17
    • 1970-01-01
    相关资源
    最近更新 更多