【问题标题】:laravel relationship between 5 tables5张表之间的laravel关系
【发布时间】:2018-12-22 16:58:31
【问题描述】:

我有 5 个表 User, Profile, Address,State,City 。需要在表之间创建关系。Address 在表中有 State id , City id 和 Profile id 。Profile 在表中有 User Id 。City 有 State Id在表中。表之间的关系怎么写

class City extends Model
{
    public function state() {
        return $this->belongsTo('App\State');
    }

    public function addresses() {
        return $this->hasMany('App\Address');
    }
}


class State extends Model
{
    public function cities() {
        return $this->hasMany('App\City');
    }

    public function addresses() {
        return $this->hasMany('App\Address');
    }
}

class Profile extends Model
{
    public function address() {
        return $this->belongsTo('App\Address');
    }
    public function user() {
        return $this->belongsTo('App\User');
    }
}

class Address extends Model
{
    public function profile() {
        return $this->belongsTo('App\Profile');
    }
    public function city() {
        return $this->belongsTo('App\City');
    }
    public function state() {
        return $this->belongsTo('App\State');
    }

}

// users table
public function profile(){
    return $this->hasOne('App\Profile');
}

【问题讨论】:

  • 请说明您面临的问题...

标签: laravel relationship


【解决方案1】:

一般来说你的模型设计是正确的,我已经编辑了一些部分。试试下面的代码。

class City extends Model
{
     public function state()  
     {
         return $this->belongsTo('App\State');
     }

     public function addresses() 
     {
         return $this->hasMany('App\Address');
     }
}


class State extends Model
{
     public function cities() 
     {
          return $this->hasMany('App\City');
     }

     public function addresses() 
     {
          return $this->hasMany('App\Address');
     }
}

class Profile extends Model
{
     public function addresses() 
     {
         return $this->hasMany('App\Address');
     }

     public function user() 
     {
         return $this->belongsTo('App\User');
     }
}

class Address extends Model
{
      public function profile() 
      {
           return $this->belongsTo('App\Profile');
      }

      public function city() 
      {
           return $this->belongsTo('App\City');
      }

      public function state() 
      {
           return $this->belongsTo('App\State');
      }
}

class User extends Model
{
     public function profile()
     {
          return $this->hasOne('App\Profile');
     }
}

顺便说一下,Laravel 关系会根据你的方法名称添加默认键。如果您对此有疑问,可以从官方文档中找到信息。例如:

$this->belongsTo('App\Model', 'foreign_key', 'other_key');

【讨论】:

    【解决方案2】:

    正如@mustafa.akcoban 所说...

    当你使用 belongsTo 时,Eloquent 的工作方式如下

    $this->belongsTo('App\City', 'foreign_key', 'other_key');
    // foreign_key = is the primary key in the related model, by default 'id' for Eloquent 
    // other_key = is the field in the current model that contains the id of the other model, by default othermodelname_id for Eloquent
    // For eg. 'App\City', 'id', 'city_id' 
    

    当你使用 hasMany Eloquent 时,工作如下

    $this->hasMany('App\Model', 'currentmodel_id', 'primary_key');
    // currentmodel_id = is the field that contains the current model primary key in the related model
    // primary_key = is the current primary key model that will be in the other model, by default id for Eloquent
    // For eg. 'App\City', 'state_id', 'id' 
    

    请记住,您可以或不能使用第二个和第三个参数,如果出现问题,Laravel 转储会告诉您在表中没有找到哪一列,您可以修复。

    请试着练习一下,然后告诉我它是如何工作的 :)

    【讨论】:

      猜你喜欢
      • 2019-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-25
      • 1970-01-01
      • 1970-01-01
      • 2015-08-02
      相关资源
      最近更新 更多