Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Mohamed_Hammad's avatar

filter data laravel

I have table called "orders" suppose that i have two columns [date, total_price], in my case i need to filter data by date, i am already filter data but i need to get all orders that created in the same date in one row and with the total price example: if i have 3 order first order date=> 16-10-2022, total price=>50 second order date=> 16-10-2022, total price=>100 third order date=> 16-10-2022, total price=>150 fourth order date=> 20-10-2022, total price=>90

i need data like that date=> 16-10-2022, total price=>50+100+150

0 likes
8 replies
Friedrich's avatar

try this simple code:

//in your controller like:
public function search_date(Request $request){
$date = Record::where('date', $request->search)->get()->sum('price');
return view('view')->with(['data'=>$date]);
}

//in your view like:
@forelse ($data as $record)
//gives the total price of the filtered date
{{$record->price}}
@empty
<p>No order Found</p>
@endforelse
kevinbui's avatar

Maybe this is gonna work:

Order::selectRaw('date, sum(total_price) as total')
    ->groupBy('date')
    ->get();
Mohamed_Hammad's avatar

@kevinbui return multiple rows that have same date, i need to merge these rows in one row with the sum of price

frankielee's avatar

@Mohamed_Hammad

Just extending the answer from @kevinbui,

Order::selectRaw('Date(created_at) as formatted_date, sum(total_price) as total')
    ->groupBy('formatted_date')
    ->get();
```	
1 like
Mohamed_Hammad's avatar

@frankielee my problem now in "group by" i pass to it created_at that is a datetime not a date how to solve this problem

Mohamed_Hammad's avatar

$done_orders = Order::selectRaw('created_at, sum(price) as total')->whereDate('created_at','=', $dataTiming['fromDate'])->where('status','done')->groupBy('created_at')->get();

Please or to participate in this conversation.