类ModelA有关系belongsTo到ModelB。有没有办法从 访问该属性ModelA?就像是:
$this->model_b->model_b_attribute;
另外,有没有办法将模型链接到属性?如果我有belongsTo从ModelB到的关系,ModelC我可以这样做:
$this->model_b->model_b_attribute->model_c;
编辑:
我的代码:
ModelA 将会:
class LeaseTenant extends Model {
protected $appends = ['is_deposit_paid'];
public function lease_request()
{
return $this->belongsTo('App\Models\LeaseRequest');
}
public function getIsDepositPaidAttribute()
{
return $this->email == $this->lease_request->security_deposit_entry->bank_account->user->email;
}
}
并且ModelB:
class LeaseRequest extends Model {
protected $appends = ['security_deposit_entry'];
public function getSecurityDepositEntryAttribute()
{
return Rent
::where('property_id', $this->property_id)
->where('lease_request_id', $this->id)
->where('type', 'security_deposit')
->orderBy('created_at', 'asc')->first();
}
}
我想Rent从LeaseTenant.
千万里不及你