对不起,如果我的问题没有意义......
我有这两个表区域,地址和通过外键相关...这里是区域和地址模型
class Area extends Model
{
protected $table='areas';
public function sections(){
$this->hasMany(Area::class,'area','id');
}
public function address(){
$this->belongsTo(Address::class,'id');
}
}
class Address extends Model
{
protected $table='address';
public function area(){
return $this->hasMany(Area::class,'id');
}
}
我有第三个表格部分连接到区域表。
我的问题可以建立一个雄辩的关系,可以从“区域表连接区域而不是地址”部分访问地址表的数据
class Section extends Model
{
protected $table ='sections';
protected $fillable =[
'sec_code',
'area',
'id',
];
public function beneficiaries(){
return $this->hasMany(Beneficiary::class,'ben_sec','id');
}
public function area_sec(){
return $this->belongsTo(Area::class,'area');
}
public function address(){
// need the eloquent relation if could
}
}
谢谢
答案 0 :(得分:2)
将其称为属性而不是方法:
$model->area_sec;
答案 1 :(得分:0)
您可以先从“区域”关系中获取数据,因此代码将如下所示:
class Section extends Model
{
protected $table ='sections';
protected $fillable =[
'sec_code',
'area',
'id',
];
public function area_sec(){
return $this->belongsTo(Area::class,'area');
}
public function address(){
return $this->area_sec->address;
}
}