2017-11-07 3 views
1

안녕하세요 누구 도와 주실 수 있습니까?질의 빌더 t에서 질의어 질의로 변환

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'); 
} 

모델 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'); 
    } 

I는 다음과 같이 시도해 봤지만 제대로 작동하지 않습니다 ```

return $journal = PersonFamily::where([ 
      ['user_id','=',$userId] 
      ]) 
      ->with('journal','journal.jurnalDetails') 
      ->get(); 
``` 

답변

0

나는 이런 식으로 뭔가 할 것 :

// A family has many family members 
public function familyMember() { 
    return $this->hasMany('App\FamilyMember'); 
} 

FamilyMember 모델 : : 당신이 가족, FamilyMember, 저널 및 JournalEntry 모델

가족 모델이 가정

// 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 외래 키가 다른 경우 더 많은 사용자 정의 관계를 만들 수 있습니다.