php - Laravel Sum value and group by

796

I have some issue, im using Laravel with DB table, i need to sum values, also display name (text field) and group by content id, the problem im getting, is that i get an mysql error saying, name is not on group by, well if i add name on group by it wont groupby content id and it will show repeat values...this is what i have:

DB::table('mytable')    
            ->addselect(DB::Raw('sum(items) as items'))
            ->addselect(DB::Raw('name as name'))
            ->addselect(DB::Raw('id as id')) 
            ->groupBy('id')
            ->get();

I need to display the name and items with sum, grouped by id.

Doing example above im getting mysql error asking to add name on groupby...witch gives me repeats values.

People are also looking for solutions to the problem: php - Get input field value without refreshing page

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.