php - group by child entity in laravel 5 with eloquent -


i have tables (simplified):

products:     id,     model_id,     color  models:    id,    name 

and want know how many products have of each model , each color, in sql can way:

select models.name, count(*)  models inner join products on (models.id = products.model_id) group products.color, products.model_id 

but can't doit eloquent, code:

model::with('products','products.model')->groupby('products.color')->groupby('products.model')->get(); 

throws error:

sqlstate[42s22]: column not found: 1054 unknown column 'products.color' in 'group statement'  

it's eloquent not knows relationship model products, i'm missing?

update: moein sends me in rigth direction, can solve doing this:

model::join('products', 'models.id', '=', 'products.model_id')                 ->selectraw('products.*, count(*)')                 ->groupby('products.color')                 ->groupby('products.modelo_id')                 -> get(); 

update: moein sends me in rigth direction, can solve doing this:

model::join('products', 'models.id', '=', 'products.model_id')             ->selectraw('products.*, count(*)')             ->groupby('products.color')             ->groupby('products.modelo_id')             -> get(); 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -