0

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?

sam67
  • 61
  • 2
  • 12
  • Hi Sam, Do you have the original query that you're using for the '$queryTwo' argument? – Robert Young Sep 24 '20 at 08:17
  • 1
    Does this answer your question? [Laravel - Eloquent "Has", "With", "WhereHas" - What do they mean?](https://stackoverflow.com/questions/30231862/laravel-eloquent-has-with-wherehas-what-do-they-mean) – Qirel Sep 24 '20 at 08:18
  • @RobertYoung $queryTwo is just part of the query builder – sam67 Sep 24 '20 at 08:24

3 Answers3

1

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();
P. K. Tharindu
  • 2,565
  • 3
  • 17
  • 34
0

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

P. K. Tharindu
  • 2,565
  • 3
  • 17
  • 34
  • I don't think its the date, looking at debug bar the query does not use the where clause in the statement. – sam67 Sep 24 '20 at 08:38
  • Try committing out the second `with()`. The first with() statement is probably overloaded by the second statement. – P. K. Tharindu Sep 24 '20 at 08:46
0

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();
sam67
  • 61
  • 2
  • 12