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

MahmoudAdelAli's avatar

sum the result of records where condition in blade

hello , i have a condition here

'invoices' => Invoice::latest()->when(request('start_date'), function ($q){
                $start_date = Carbon::parse(request('start_date'))->toDateTimeString() ?? '';
                $end_date = Carbon::parse(request('end_date'))->toDateTimeString() ?? '';
                return $q->whereBetween('created_at',[$start_date,$end_date]);
            })->paginate(10),

Edit I do not want to repeat the query again to sum where type 1 and the coming result is correct and i need to sum amount invoice where type = 1 so i have created this method inside model

 public function getTotalRevenuesAttribute()
    {
        return $this->whereType('1')->sum('amount');
    }

and work correctly - but when i use the date filter and get 1 or 2 rows or any count the total revenues still static i mean the method sum the revenues for all table regardless of records coming from invoices - and i use the if condition

@if($invoice->type == 1)
{{$invoics->sum('amount')}}
@endif	

and still sum all records !

0 likes
13 replies
tykus's avatar

I don't understand what the accessor method is supposed to be doing here. What model is this defined on; and why would a single instance of the model calculate the sum from all records?

1 like
MahmoudAdelAli's avatar

@tykus cause i want to use this method in many pages and many routs so i insert it into model to invoke it any time i need - and it works correctly but when the records change cause i change the date still sum all where type =1

tykus's avatar

@MahmoudAdelAli so you have a single arbitrary Invoice model instance; and on that instance you can get the total_revenue property of all Invoice instances (where type is 1)? Why?

MahmoudAdelAli's avatar

@tykus :) cause the type 1 mean revenues and type 2 mean Expenses - so i need to sum all revenues and print it and sum all Expenses and print it and subtract the two sum and get the profits - why ? cause the client need that so now the data is correct and every thing is ok but i need to apply all that when date filter work

MahmoudAdelAli's avatar

@tykus thank you i appreciate that but imagine i need to use this $total in many different places and controllers ? what the solution here ? and second thing it's not the problem , the issue here the where not work inside blade and i cant repeat the code many times to get the sum where type between 2 dates

  return view('admin.invoices.index',[
            'invoices' => Invoice::latest()->when(request('start_date'), function ($q){
                $start_date = Carbon::parse(request('start_date'))->toDateTimeString() ?? '';
                $end_date = Carbon::parse(request('end_date'))->toDateTimeString() ?? '';
                return $q->whereBetween('created_at',[$start_date,$end_date]);
            }),

and when i use where inside blade

{{$invoices->where('type' , 2)->sum('amount')}}

i got

SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'where clause' 
MahmoudAdelAli's avatar
MahmoudAdelAli
OP
Best Answer
Level 5

The solution i found for now - is use latest() with get()

return view('admin.invoices.index',[
            'invoices' => Invoice::latest()->when(request('start_date'), function ($q){
                $start_date = Carbon::parse(request('start_date'))->toDateTimeString() ?? '';
                $end_date = Carbon::parse(request('end_date'))->toDateTimeString() ?? '';
                return $q->whereBetween('created_at',[$start_date,$end_date]);
            })->get()->paginate(10),


        ]);

to decrease the quires and in blade

{{$invoices->where("type", 2)->sum('amount')}}

so here from 11 quire to 1 quires :D

tykus's avatar

@MahmoudAdelAli there's nothing that will convince you to make a separate query to compute the sum; instead you are fetching every Invoice record?

antaugustol's avatar

@mahmoudadelali, I think you have to use the get() method to get a collection where you can apply the method sum(). I tried it here using dd($invoices) and without get() I have a Illuminate\Database\Eloquent\Builder, while with get() I have a Illuminate\Database\Eloquent\Collection, which is what you need.

1 like

Please or to participate in this conversation.