packy
5 months ago
1016
12
Eloquent

relationship groupBy in initial query

Posted 5 months ago by packy

I am trying to groupBy a relationship in the initial query from my controller but not having such luck. For now I have a workout, but seems clunky:

public function menu(Location $location)
    {
        $location->entrees = $location->entrees->groupBy(function ($entree) {
            return $entree->course->name;
        });

        return view('food-order.menu', compact('location'));
    }

I thought it would look something like this

public function menu(Location $location)
    {
       $location->load(['entrees' => function ($q) {
            $q->groupBy(function ($entree) {
                return $entree->course->name;
            });
        }]);

        return view('food-order.menu', compact('location'));
    }

but get the error: stripos() expects parameter 1 to be string, object given. Is this way off target of how I should do this???

Please sign in or create an account to participate in this conversation.