我有这样的查询:
public function getByUser($userId,$activityId){
return $journals = DB::table('personfamilies')
->join('journals','personfamilies.id','=','journals.person_id')
->join('journal_details','journals.id','=','journal_details.journal_id')
->where([
['user_id','=',$userId],
['activities_id','=',$activityId]
])
->get();
}
如何转换为雄辩?
模型PersonFamily.php
public function relationship(){
return $this->hasOne(Relationship::class,'id','relationship_id');
}
public function user(){
return $this->belongsTo(User::class);
}
public function journal(){
return $this->hasMany(Journal::class,'id','id');
}
Model Journal.php
public function jurnalDetails(){
return $this->hasMany(JurnalDetail::class,'id');
}
public function personfamily(){
return $this->belongsTo(PersonFamily::class,'person_id','journal_id');
}
JournalDetail.php
public function journal(){
return $this->belongsTo(Journal::class,'id');
}
我已经尝试过这样,但它无法正常工作 ```
return $journal = PersonFamily::where([
['user_id','=',$userId]
])
->with('journal','journal.jurnalDetails')
->get();
```
答案 0 :(得分:0)
我会做这样的事情:
假设您有Family,FamilyMember,Journal和JournalEntry Models
家庭模式:
// A family has many family members
public function familyMember() {
return $this->hasMany('App\FamilyMember');
}
FamilyMember型号:
// A family member belongs to exactly one family
public function family() {
return $this->belongsTo('App\Family')
}
// A family member has exactly one journal
public function journal() {
return $this->hasOne('App\Journal');
}
期刊模型:
// A journal belongs to exactly one family member
public function familyMember() {
return $this->belongsTo('App\FamilyMember');
}
// A journal has many journal entries
public function journalEntries() {
return $this->hasMany('App\JournalEntry');
}
JournalEntry模型:
// A journal entry belongs to exactly one journal
public function journal() {
return $this->belongsTo('App\Journal');
}
然后你的查询变得相当简单:
// If you need the whole Family
$family = Family::find($familyId);
// If you need all the family members that belong to a family
$familyMembers = $family->familyMembers;
// If you are looking for a particular family member
$familyMember = FamilyMember::find($userId);
// If you need the journal for a family member
$journal = $familyMember->journal
// If you need all the journal entries in a particular journal
$journalEntries = $journal->journalEntries;
然后,您可以将这些变量传递到视图中并根据需要显示它们。
See the documentation以便在外键不同的情况下建立更多自定义关系。