Compose a groupby record with avg in Eloquent

180
May 06, 2018, at 11:02 PM

I have this SQL query: SELECT *, AVG(value) as avg FROM users_has_project GROUP BY project_id And I have translated in Eloquent in the following way:

Vote::select('AVG(value) as avg')->groupBy('project_id')->get();

But I received the following error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'AVG(value)' in 'field list' (SQL: select AVG(value) as avg from users_has_project group by project_id)

Answer 1

Try using a raw query for the aggregate:

Vote::selectRaw('AVG(value) as avg')->groupBy('project_id')->get();
Answer 2

Just a tiny enhancement in @DigitalDrifter's answer:

Vote::selectRaw('project_id, AVG(value) as avg')->groupBy('project_id')->get();
Rent Charter Buses Company
READ ALSO
merge two columns into an array

merge two columns into an array

I want to merge two of my columns (yanlis_cevaplar, cevap_icerik) into an array and this code here gives me only one column in array when I print it (yanlis_cevaplar)

161
This page isn’t working. domain.com is currently unable to handle this request. HTTP ERROR 500

This page isn’t working. domain.com is currently unable to handle this request. HTTP ERROR 500

I try to login my web siteIt cannot login show error this page isn't working is currently unable to handle this request

151
PHP how to get contents from uploaded text file

PHP how to get contents from uploaded text file

Hi guys i have this simple PHP upload function in html but i want to use let the user upload their text file and it will show the contents of it in the next page i know i have to use the file_get_contents function but how do i combine it with the upload...

144
laravel - cross join not returning all columns

laravel - cross join not returning all columns

This is my table in mysql

180