【问题标题】:Nested eager loading in where clause in Laravel在 Laravel 的 where 子句中嵌套急切加载
【发布时间】:2016-06-14 21:58:03
【问题描述】:

我想知道无论如何在 Laravel 中使用 Eloquent 编写这样的查询。我的意思是我可以在 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();

【问题讨论】:

    标签: orm eloquent laravel-5.1 eager-loading


    【解决方案1】:

    据我所知,您可以像 $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');
    

    【讨论】:

      猜你喜欢
      • 2020-09-12
      • 1970-01-01
      • 1970-01-01
      • 2017-09-30
      • 2019-07-29
      • 1970-01-01
      • 1970-01-01
      • 2019-11-05
      • 1970-01-01
      相关资源
      最近更新 更多