lundi 29 août 2016

Laravel 5 group by relationship's column

I am using Laravel 5.2. I have 2 models : Vehicle and Brand

Eloquent Relationships are as follows:

Vehicle belongsTo Brand

Brand hasMany Vehicles

I wanted the count of vehicles in each Brand with Brand name after applying a set of filters to the vehicles list.

My code:

        $result = Vehicle::ApplyFilters($conditions)  // this is a scope applied initially
                ->with(['brand' => function($query) {
                    return $query->groupBy('brand_name');
                }])->get();

Unfortunately not getting desired result. It is listing the vehicles and not grouped by brand.

I am a newbie. Any idea to make it work?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire