第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Laravel - 從另一個(gè)模型獲取模型屬性

Laravel - 從另一個(gè)模型獲取模型屬性

PHP
牛魔王的故事 2021-06-22 17:01:03
類ModelA有關(guān)系belongsTo到ModelB。有沒有辦法從 訪問該屬性ModelA?就像是:$this->model_b->model_b_attribute;另外,有沒有辦法將模型鏈接到屬性?如果我有belongsTo從ModelB到的關(guān)系,ModelC我可以這樣做:$this->model_b->model_b_attribute->model_c;編輯:我的代碼:ModelA 將會(huì):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.
查看完整描述

1 回答

?
千萬里不及你

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超9個(gè)贊

如果您belongsTo在ModelA和之間有關(guān)系ModelB:


# ModelA.php


public function modelB()

{

    return $this->belongsTo(ModelA::class);

}

然后您還可以訪問關(guān)系以獲取ModelA實(shí)例,您可以從中訪問ModelA屬性。


$modelA = ModelA::find(1);

$name = $modelA->modelB->name;

//                      ^^^^^^ modelB attribute

此外,如果您在 ModelB 中還有另一個(gè)屬于關(guān)系,您可以這樣做:


$name = ModelA::find(1)->modelB->modelC->name;

//                                      ^^^^^^ modelC attribute


查看完整回答
反對(duì) 回復(fù) 2021-06-25
  • 1 回答
  • 0 關(guān)注
  • 256 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)