【问题标题】:hasMany, belongsTo, belongsToMany parameters with custom names?具有自定义名称的 hasMany、belongsTo、belongsToMany 参数?
【发布时间】:2021-04-12 07:36:28
【问题描述】:

我有这些数据库表:

author (atr_id pk)                              // model : Author
category (ctg_id pk)                            // model : Category
post (pst_id pk, pst_atr_id fk)                 // model : Post
post_categories (pct_pst_id fk, pct_ctg_id fk)  // pivot : PostCategories 

如何定义自定义名称的hasMany、belongsTo、belongsToMany参数?

型号:作者

public function getPosts()
{
    return $this->hasMany('App\Post', ?, ?);
}

型号:帖子

public function getAuthor()
{
    return $this->belongsTo('App\Author', ?, ?);
}

public function getCategories()
{
    return $this->belongsToMany('App\Category', ?, ?, ?);
}

型号:类别

public function getPosts()
{
    return $this->belongsToMany('App\Post', ?, ?, ?);
}

【问题讨论】:

  • 显示这些表的数据库结构?
  • 你的意思是自定义外键?

标签: laravel laravel-5 eloquent


【解决方案1】:

型号:作者

<?php

public function getPosts(){
     return $this->hasMany('App\Post','pst_atr_id');// author table's foreign key in Post table.
}

模型:帖子

<?php

 public function getAuthor(){
     return $this->belongsTo('App\Author','pst_atr_id','atr_id'); // first is foreign key of author table in this current table and second is primary key name of author table.
 }

 public function getCategories(){
     return $this->belongsToMany('App\Category','post_categories','pct_pst_id ','pct_ctg_id'); /* 'post_categories' is the pivot table. Rest 2 parameters- first is foreign key present in post_categories of the current model, second is foreign key present in post_categories of the model we are relating with*/
 }

型号:类别

<?php

 public function getPosts(){
      return $this->belongsToMany('App\Post','post_categories ','pct_ctg_id','pct_pst_id');

    /* 'post_categories' is the pivot table. Rest 2 parameters- first is foreign key present in post_categories of the current model, second is foreign key present in post_categories 
     of the model we are relating with*/
 }

【讨论】:

    【解决方案2】:
    Author   : $this->hasMany('App\Post', 'pst_atr_id', 'atr_id');
    
    Post     : $this->belongsTo('App\Author', 'pst_atr_id', 'atr_id');
    
    Post     : $this->belongsToMany('App\Category', 'post_categories', 'pct_pst_id', 'pct_ctg_id');
    
    Category : $this->belongsToMany('App\Post', 'post_categories', 'pct_ctg_id' 'pct_pst_id');
    

    【讨论】:

      【解决方案3】:

      型号:作者

      public function getPosts()
      {
          return $this->hasMany('App\Post', 'pst_id', 'atr_id');
      }
      

      型号:帖子

      public function getAuthor()
      {
          return $this->belongsTo('App\Author', 'pst_atr_id', 'pst_id');
      }
      
      public function getCategories()
      {
          return $this->belongsToMany('App\Category', 'post_categories', 'pct_pst_id', 'pct_ctg_id');
      }
      

      型号:类别

      public function getPosts()
      {
          return $this->belongsToMany('App\Post', 'post_categories', 'pct_ctg_id', 'pct_pst_id');
      }
      

      【讨论】:

        猜你喜欢
        • 2018-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多