Skip to content
Advertisement

Laravel query not using where clause for date

I have made a laravel query which gets all the data but does not use the where clause for $startdate.

$user->studentGroups()->with([
            'GroupTasks' => function ($queryTwo) use ($startdate)

    { return $queryTwo->where('start_date', '>', $startdate); } // THIS LINE IS NOT WORKING!!

    ])->with(['GroupTasks.prgressList' => function ($queryThree) use ($student_id)
    { return $queryThree->where('progress_student_task_users.mis_id', $student_id); },
        'GroupTasks.studentViews' => function ($queryfour) use ($student_id){ return $queryfour->where('student_task_user_vieweds.mis_id', $student_id)->latest(); },
    ])->get();

Without the start date working it just gets all the data which is not really the idea. Any idea how to fix?

Advertisement

Answer

Try this:

$user->studentGroups()
    ->with([
        'GroupTasks' => function ($query) use ($startdate, $student_id) {
            return $query->whereDate('start_date', '>', $startdate)
            ->with([
                'prgressList' => function ($query) use ($student_id) {
                    return $query->where('progress_student_task_users.mis_id', $student_id);
                },
                'studentViews' => function ($query) use ($student_id) {
                    return $query->where('student_task_user_vieweds.mis_id', $student_id)->latest();
                },
            ]);
        }

    ])->get();
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement