2016-05-28 16 views
2

私はララベルのモデル関係について疑問を持ち、雄弁なクラスでそれらを使用します。私は別の関係を持っているtransferFormでもlaravel 5.1:モデルからネストされたリレーションを取得するにはどうすればよいですか?

public function transferFormsContent() 
{ 
    return $this->hasMany('App\TransferFormsContent', 'form_id', 'id'); 
} 

:私はこのようなモデル持って

class TransferFormsContent extends Model 
{ 

public function transferForm() 
    { 
     return $this->belongsTo('App\TransfersForms','form_id'); 
    } 
} 

をしてtransferFormに私はこのように、この関係のための他の側面を持っている私の質問はこれですこのような従業員の場合:

class TransfersForms extends Model 
{ 
    public function employee() 
     { 
      return $this->belongsTo('App\User','employee_id'); 
     } 
} 

「転送」 FormsContent "とその" transferForm "が従業員と共に提供されています。これどうやってするの?私は唯一の「transferForm」と「TransferFormsContent」を取得したい場合は

私は今、私はこの中から使用することができます

$row = $this->model 
      ->with('transferForm'); 

をどのように私はtransferFormもその従業員と一緒にしたい場合は?

+0

でソートまたはフィルタする必要があるとき

$row->$this->model ->with(['transferForm' => function($q) { $q->with('employee') }]); 

第二の方法が便利です[hasManyThrough](https://laravel.com/docs/5.2/eloquent-relationships#has-manythrough)はあなたを助けますか? – Rifki

答えて

2

[OK]を私はそれを見つける:

$row = $this->model 
      ->with('transferForm.employee') 

今あなたがその「transferForm」と「TransferFormsContent」からのレコードは、従業員とそのを提供している:

だけで、あなたがこれをすることを行うことができます。

1

あなたは、ドット表記とネストされた積極的なロードを使用することができます

$row = $this->model 
     ->with('transferForm.employee'); 

それとも、クロージャを使用することができます:あなたは、従業員

関連する問題