AbdulBazith's avatar

Problem in sum(fields) in pagination laravel

Guys iam working with project.

i have given pagination and made to view.blade.

i also add the total values in a single field and passed to view.blade that is sum('amount') where amount is column name in my table.

My problem is the sum of the values are displayed page wise in the view.blade

i need the grand total must be displayed in the end of the page thats enough.

that is if i have 10 records and give pagination(5) then for the first five records the total is displayed in the first page and the next five records total is displayed in the second page.. i need to display the grand total of all the 10 records and must be displayed in the last page. because the user needs to add the first page total and second page total to get the grand total

So kindly some one suggest an idea please??

this is my controller

public function index()
    {
        $exs = Excess_milk::orderBy('created_at','desc')->paginate(5);

        return view('Excess_milk.view', [
            'exs' => $exs,

            'no_of_litre_total' => $exs->sum('no_of_litre')
        ]);
     
    }

this is my screen shots

Page 1 Refer: https://imgur.com/XiWhGA9

Page 2 refer : https://imgur.com/IPzeJ2F

kindly someone help to rectify this

0 likes
12 replies
pardeepkumar's avatar

i think you should try like this

$query = Employee::whereBetween('demo', array($bcs, $fcs));

$sum = $query->sum('bccs'); 

$ventas = $query->orderBy('demo', 'DSC')->orderBy('bcvd', 'ASC')->paginate();
1 like
Snapey's avatar

you can sum the page total before returning to the view, and also by inspecting the paginator, decide if it is the last page.

If so, sum all the records with a separate query and return that to the view as a grand total.

caching the value might be an idea.

1 like
AbdulBazith's avatar

@pardeepkumar thankz for your response

i tried your code it worked. But the grand total is displayed in each page..

"If so, sum all the records with a separate query and return that to the view as a grand total."

@Snapey if you dont mind can you slightly explain what you said with example please..

"If so, sum all the records with a separate query and return that to the view as a grand total." ----> this you said.. but grand total is displayed in each page..

you can sum the page total before returning to the view, and also by inspecting the paginator, decide if it is the last page.--

this i understood , but how??

please suggest some idea

1 like
Snapey's avatar

After the paged query;

if($exs->lastPage() == $exs->currentPage()) {
    
    //calculate the grand total

    $grandTotal = ....
}

in the view

    @if($grandTotal)

        <tr><td>{{ $grandTotal }}</td></tr>

    @endif
AbdulBazith's avatar

@Snapey i did this but not working showing error

my controller


  $exs = Excess_milk::orderBy('created_at','desc')->paginate(5);

        if($exs->lastPage() == $exs->currentPage()) 
        {        
            $grandTotal =  $exs->sum('no_of_litre');
        }

        return view('Excess_milk.view', [
            'exs' => $exs,
            'grandTotal' => $grandTotal 
        ]);

this is my view


@if($grandTotal)
            <tr ><td colspan='2' align="center"><b> Total</b></td>
                <td ><b>{{ $grandTotal}}</td>
                <td colspan='2'></td>
             </tr>
             @endif

But showing error

Undefined variable: grandTotal

whats the problem

Snapey's avatar

On pages other than the last, you don't have a $grandtotal to check, so perhaps in the view;

@if(isset($grandTotal))
            <tr ><td colspan='2' align="center"><b> Total</b></td>
                <td ><b>{{ $grandTotal}}</td>
                <td colspan='2'></td>
             </tr>
             @endif

AbdulBazith's avatar

@Snapey my undefined variable is showing in my controller


        return view('Excess_milk.view', [
            'exs' => $exs,
 
            'grandTotal' => $grandTotal 
        ]);
 
        // return view('Excess_milk.view')->withexs($exs);
    }

the error is shown in controller not in view.blade

andi also have some other doubts please visit that post also and reply please

Refer:

How to pass the sum('fields') to excel conversion using Maatwebsite in laravel

How to return the excel download to ajax ??

Problem in pagination after ajax return in laravel

Problem in ajax return date format, pagination and buttons (edit and delete) in laravel

Snapey's avatar
Snapey
Best Answer
Level 122

You can conditionally pass data to the view by using view::share

  $exs = Excess_milk::orderBy('created_at','desc')->paginate(5);

        if($exs->lastPage() == $exs->currentPage()) 
        {        
            View::share('grandTotal', $exs->sum('no_of_litre'));
        }

        return view('Excess_milk.view', [
            'exs' => $exs
        ]);

but your total is wrong. Here you only add the rows in the paginated data, but you need to run a new query summing all the records.

2 likes
AbdulBazith's avatar

@Snapey really you are great thank youu soo much it worked

i did as per you suggestion

  $all=Excess_milk::orderBy('created_at','desc')->get();

 $exs = Excess_milk::orderBy('created_at','desc')->paginate(5);

        if($exs->lastPage() == $exs->currentPage()) 
        {        
            View::share('grandTotal', $all->sum('no_of_litre'));
        }

        return view('Excess_milk.view', [
            'exs' => $exs
        ]);

worked thank youuu so much

Kindly suggest

for just two question please

refer:

Problem in ajax return date format, pagination and buttons (edit and delete) in laravel

How to return the excel download to ajax ??

Please suggest an idea

1 like
Snapey's avatar

Still not quite right. You execute the query for $all every page and not just on the last page, and for sum the order is not relevant

You can also do aggregate methods directly on the query rather than on the collection


        $exs = Excess_milk::orderBy('created_at','desc')->paginate(5);

        if($exs->lastPage() == $exs->currentPage()) 
        {        

            View::share('grandTotal', Excess_milk::sum('no_of_litre'));
        }

        return view('Excess_milk.view', [
            'exs' => $exs
        ]);

1 like
AbdulBazith's avatar

@Snapey your code worked,, thank you yesterday itself your code worked. i have mentioned the code above,, it worked.

iam having doubt in another questions

if those solved my problems are solved

refer:

Problem in ajax return date format, pagination and buttons (edit and delete) in laravel

How to return the excel download to ajax ??

Please suggest an idea please please..

Please or to participate in this conversation.