Reputation: 61
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?
Upvotes: 0
Views: 194
Reputation: 2730
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();
Upvotes: 1
Reputation: 61
If I move the ->get() to just after the where clause it gets the data including start date but does not include the rest of the with's!
$user->studentGroups()->with([
'GroupTasks' => function ($queryTwo) use ($startdate)
{ return $queryTwo->where('start_date', '>', $startdate); } // THIS LINE IS NOT WORKING!!
])->get() // ADD GET() HERE NOW WORKS
->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();
Upvotes: 0
Reputation: 2730
Try using whereDate()
instead.
return $queryTwo->whereDate('start_date', '>', $startdate)
Also, try casting start_date
fields to date by adding something like below to your GroupTask
model:
protected $dates = [
'start_date',
'end_date'
];
You can read more about attribute casting here: https://laravel.com/docs/8.x/eloquent-mutators#date-mutators
Upvotes: 0