2016-03-02 31 views
1

我想知道是否有在Laravel中使用Eloquent來寫這樣的查詢。我的意思是我可以在where子句中進行熱切加載。我從最後一天卡在這裏,我真的想這樣做。所以需要建議。在Laravel的where子句中嵌套的熱切加載

 $notifications = Notification::where('user_id',Auth::user()->id) 
      ->where('is_try', '!=', Config::get('constants.NOTIFICATION_AGAINST_JOB_TURN_DOWN')) 
      ->where('is_try', '!=', Config::get('constants.NOTIFICATION_AGAINST_JOB_EXPIRED')) 
      ->where(function($query) { 
       $query->where(function($query) { 
        $query->where('message', '!=', 'job_post') 
         ->with(['suggestion' => function ($query) { 
          $query->with(['job' => function ($query) { 
           $query->with('company'); 
          }]); 
         }]); 
        }) 
        ->orWhere(function($query){ 
         $query->where('message','job_post') 
         ->with(['job' => function ($query) { 
          $query->with('company'); 
         }]); 
       }); 
      }) 
      ->orderBy('notifications.created_at','desc') 
      ->get(); 

回答

0

按我的知識,你可以加載的關係像這樣$query->load('company');代替$query->with('company');

也可以加載像這樣多/嵌套關係。

Notification::with([ 
        'rel'=>function($q){ $q->where()... //can add up where, orderby etc clauses like this}, 
        rel.profile, 
        rel.profile.images, 
        rel.profile.images.location 

        ])->wehre().... 

,你也可以做到這一點,

$course_orignal  = Courses::find($course_id); 

$course_orignal->load('quizzes','quizzes.questions', 'chapters','chapters.quizzes.questions','chapters.lessons','chapters.lessons.quizzes','chapters.lessons.quizzes.questions');