How to compare in with() relation with parent data like WhereHas do

Viewed 47

I make a request like this :

Event::where('team_id', Auth::user()->currentTeam->id)
->whereHas('animals', function ($query) use ($decodedId) {
    $query->where('animals.id', $decodedId)
        ->whereHas('health_items', function ($query) {
            $query->whereColumn('date_end', 'events.end');
        });
})

Thanks to whereColumn, I compare my data health_items with that of the grandparent events

$query->whereColumn('date_end', 'events.end');

It works.

Only with WhereHas, I don't have the relations data in my response (it just filters out the events)

I try to do the same by adding a with it doesn't work.

Event::where('team_id', Auth::user()->currentTeam->id)
->with(['animals' => function($query) use ($decodedId) {
        $query->where('animals.id', $decodedId)
            ->with(['health_items' => function ($query) {
                $query->whereColumn('date_end', 'events.end');
            }]);
    }])

Returned error : Unknown column 'events.end'

Is there a way to filter the "health_items" items using a column in the events table?

Thank you

0 Answers
Related