vendredi 26 août 2016

Laravel 5 Eloquent - group by hasmanythrough relationship column

I have 3 models : Vehicle, Dealer and Province

Eloquent Relationships are as follows:

Vehicle belongsTo Dealer

Dealer hasMany Vehicles

Province hasMany Dealers

Dealer belongs to Province

Province hasManyThrough Vehicles

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

My code:

        $result = Vehicle::ApplyFilters($conditions)
                ->with(['dealer.province' => function($query) {
                    return $query->groupBy('province_name');
                }])->get();

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

Any idea to make it work?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2bUoNC1
via IFTTT

Aucun commentaire:

Enregistrer un commentaire