簡體   English   中英

Laravel改變雄辯的級聯序列

[英]Laravel change eloquent cascading sequence

我有$persons集合,其中包含具有聯系的$person雄辯對象模型的數組。 一個人有多個帳戶,一個帳戶有一個en。

例如,如果要訪問en的e1屬性,則為: $person->accounts[0]->en

如何轉換$person級聯序列:

person=>{
  p1,
  p2,
  accounts{
    a1,
    a2,
    en{
      e1,
      e2
    }
  }
}

變成類似:

en{
  e1,
  e2,
  accounts{
    a1,
    a2
  },
  person{
    p1,
    p2
  }
}

更改后,我想要這樣的東西$en->accounts->person->p1

您可以在En模型中創建一個關系,以指定與Account模型的關系。

public function account(){
        return $this->belongsTo(Account::class);
    }

並在“帳戶”模型中,創建與“人”模型的關系。

public function person(){
        return $this->belongsTo(Person::class);
}

然后,您將能夠按要求檢索數據

//en should have account and account should be associated with person
$en->accounts->person->get();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM