mardi 4 septembre 2018

Laravel: Querying a Many-to-Many relationship

I got a project with the Models Menu and User, which are in a Many-to-Many relationship.

On one site I show the past menus per calender week, on the other I want to show the order history of a specific user per calender week.

I got the menu history working with the following query:

public static function archives()
    {
        return  Menu::selectRaw('week(date, 1) as kw, year(date) as year')
                ->orderByRaw('min(date) asc')
                ->groupBy('kw', 'year')
                ->get();
    }

Now i thought about the same query for the User, but I get errors that are not even in my statement.

public static function archives(int $id)
    {
        return User::find($id)->menus()
                    ->selectRaw('week(date, 1) as kw, year(date) as year')
                    ->orderByRaw('min(date) asc')
                    ->groupBy('kw', 'year')
                    ->get();

    }

Expression #3 of SELECT list is not in GROUP BY



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire