Converting multiple same name into one single name and also values get sum in laravel

Viewed 780

I am trying to fetch the multi same name to single name also sum associated QTY values also.Show PART-ID as it's

example

enter image description here

I want to show like this

enter image description here

my controller

    public function productserach()
{ 
    $searchitem = Item_list::get();
    return view('products.product-serach',compact('searchitem'));
}
2 Answers

you can do try this code

use DB;

$sums = Model::select(DB::raw('sum(qty) as total_quantity'),'name')->groupBy('name')->get();
DB::table('your_table')->select(DB::raw('sum(qty) as qty,name,part_id'))->groupBy('name','part_id')->get()

So basically use raw select to sum qty you want and group by fields that you want to have unique. Also you can use laravel eloquent like above but change it a bit because it is incorrect the output data will be not same as you want.

Related