laravel 5张桌子之间的关系

时间:2018-12-22 16:58:31

标签: laravel relationship

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

2 个答案:

答案 0 :(得分:0)

通常,您的模型设计是正确的,我已经编辑了一些部分。请尝试以下代码。

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

答案 1 :(得分:0)

如@ 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' 

当您使用hashasEloquent作品时,如下所述

$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转储会告诉您表中未找到哪一列,您可以进行修复。

请尝试实践,让我知道它是如何工作的:)