2016-07-19 10 views
0

を子(関係を)返す私が働いているモデルは、このような構造とする多ワンです。Laravelはだけでなく、親

私がすべてを取得したい場合UserNotes私はそのユーザーに属するメモを取得するためにチェーンをフォローする必要があります。

私の現在のモデルはこれを行いますが、必要以上に多くの情報(ディストリビューター)を返します。 Notesとそれが属しているAccountを返すだけでいいです、Distributorを取得する必要はありません。

コントローラー:

public function showManager($id) 
    { 
     $user   = User::find($id); 
     $distributors = User::find($id)->distributorsWithAccounts; 
     $notes   = User::find($id)->notes; 

     //dd($notes); 

     return view('admin/managers-single', 
     [ 
      'user'   => $user, 
      'distributors' => $distributors, 
      'notes'   => $notes, 
     ]); 
    } 

ユーザーモデル:

public function notes() 
    { 
     return $this->hasMany('App\Distributor')->with('accountsWithNotes'); 
    } 

ディストモデル:

public function accountsWithNotes() 
    { 
     return $this->hasMany('App\Account')->has('notes')->with('notes'); 
    } 

勘定モデル:

public function notes() 
    { 
     return $this->hasMany('App\Note'); 
    } 

答えて

0

実際LaravelにはhasManyThroughというクエリがあります。だから私は、そう見えるように私のUser Model functionを変更:

public function notes() 
    { 
     return $this->hasManyThrough('App\Account', 'App\Distributor')->has('Notes')->with('Notes'); 
    } 

これは今ちょうどあなたがこの のようにただ一つのクエリで私の友人のすべてのデータを持つことができ、その情報だけでなく

0

Notesを持ってAccountsをダンプUser::whereId($id)->with('distributors.notes')私はあなたのモデルがまったく関連しているのか分かりませんが、私はモデルをユーザー、ロール、アクセス許可を私のユーザモデルに持っています。私はロールというメソッドを持っています。ロールメソッドではpermissionsという関数があります。

私は、クエリUser::whereId($id)->with('role.permissions')でそれらを呼び出すことができますし、答えは{user_propertes...., role:{role_properties..., permissions:[{},{},{},{}]}}

ようになります
関連する問題