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

AbdulBazith's avatar

How to return more than one variable to ajax from controller

Guys iam working with a project.

This is my controller

 $xcess_milks = Excess_milk::get();

               
      return $xcess_milks;



this my ajax


<script type="text/javascript">

        success:function(data)
        {

            var res='';
            $.each (data, function (key, value) {
            res +=
            '<tr>'+
                '<td>'+value.date+'</td>'+
                '<td>'+value.time+'</td>'+
                '<td>'+value.no_of_litre+'</td>'+
                '<td>'+value.note+'</td>'+
                '<td>'+ "<a href='{{ route('Excess_milk.show',$ex->id) }}'  class='btn btn-info' style='width: 40%; float:left; margin-right:5px;'>Edit</a>"+

                "<a href='{{ route('Excess_milk.show',$ex->id) }}' class='btn btn-info' style='width: 30%; '>Delete</a>"


                +'</td>'+
            '</tr>';

   });
            $('tbody').html(res);
        }


    });
    })

here iam just returning $xcess milk to the the ajax and palced it in the table row.

i need to pass the total value also the sum

$sumvalue=$xcess->sum('no_of_litre')

i need to sum this need to return it the ajax return and also need to paste it as last row?

How to do this??

and also how can i add pagination instead of get() in the controller.

kindly some one suggest please

0 likes
12 replies
AbdulBazith's avatar

@Artak thankz for you response..

i reffered those sites you mentioned. but my problem is after pagination how could i use it in ajax success

1 like
_Artak_'s avatar
_Artak_
Best Answer
Level 14

@AbdulBazith if i understand correctly How to return more than one variable, you want to pass multiple values, so you can do like this


return response()->json(['content'=>$xcess_milks, 'second_data_name'=>'second_data_value']);


// return response()->json([data], $status_code);  // $status_code = 200



and in js do 

success:function(data)
{

    data.content
    data.second_data_name
}

1 like
AbdulBazith's avatar

@Artak

thank you soo much it worked. this i can use for multiple variable

but my problem is in pagination also

if i use this is controller

 $xcess_milks = Excess_milk::get();
               
      return $xcess_milks;


iam using this in my ajax return to html

  success:function(data)
        {

            var res='';
            $.each (data, function (key, value) {
            res +=
            '<tr>'+
                '<td>'+value.date+'</td>'+
                '<td>'+value.time+'</td>'+
                '<td>'+value.no_of_litre+'</td>'+
                '<td>'+value.note+'</td>'+
                '<td>'+ "<a href='{{ route('Excess_milk.show',$ex->id) }}'  class='btn btn-info' style='width: 40%; float:left; margin-right:5px;'>Edit</a>"+

                "<a href='{{ route('Excess_milk.show',$ex->id) }}' class='btn btn-info' style='width: 30%; '>Delete</a>"


                +'</td>'+
            '</tr>';

   });
            $('tbody').html(res);
        }


    });
    })


But i need to use paginate(5);

then in ajax how should i change?

i think you understood my question

if not kindly refer my another thread(question). there i have clearly mentioned. if possible answer there itself please..

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

1 like
_Artak_'s avatar

you can use skip() take(), so from ajax reuqest send page length. ex.

//----------------------------------------------
// first call   (ex. page 1)

// ajax param 
$.ajax({
// ...
data: {start: 0, length: 5},
//
})

// php controller

$take = $request->take;
$skip = $request->skip;

$xcess_milks = Excess_milk::skip($skip)->take($take);   // it will skip 0 and take 5 rows;

// return section

// --------------------------------------------
// second call (ex. page 2)

// ajax param 
$.ajax({
// ...
data: {start: 5, length: 5},
//
})


// php controller

$take = $request->take;
$skip = $request->skip;

$xcess_milks = Excess_milk::skip($skip)->take($take);   // it will skip 5 rows and take  another 5 rows;

// return section

// and so on

and check this

https://datatables.net/examples/data_sources/ajax

1 like
AbdulBazith's avatar

@Artak

Thank you soo much.. that too worked just i need pagination

so i used ur code like this

$xcess_milks = Excess_milk::take(5)->get();

it worked. but is take() and paginate() are same?

why iam asking this because see i have given take(5).

so my search data may be 7 records. so it returns 5 records. if i move to next page to see my next two records then the page is refreshed.. so what should i doo??

1 like
_Artak_'s avatar

@AbdulBazith you can't use paginate for ajax request, and skip(), take() is more flexible; (for laravel 5.6 https://laravel.com/docs/5.6/eloquent-resources#pagination)

lets imagine you have 7 records, and 5 records per page, then you have 2 pages, first will show 5 and second 2 records;

you don't need to refresh the page, just add event listener and every time when you want to change a page just send ajax request and pass page value.

ex,

page 1;

pass ajax params start 0, length 5;

page 2 ajax params start 5, length 5;

this will prevent page from reloading

1 like
AbdulBazith's avatar

@Artak

Thankz for your explanation.

actually iam having tow search boxes..

say for example in the search box i will type 'Abdul' i have written on change even ajax call.

so this abdul name is taken to controller and search is done. so now in the name abdul it returns 7 records. but i have given take(5).

so first 5 records of name abdul is displayed in page 1

The next two records of abdul isdisplayed in page 2.

All this hapens through ajax request.

so if i need to see the next page 2 records and i click the page 2. my search key word disappears and page is refreshed.. iam unable to search that..

This is what my problem

Kindly reply for this..

1 like
AbdulBazith's avatar

@Artak

means?? i cant understand tags for page links means?

i think i dont have..

first i dont know what is that.

how can i create it??

AbdulBazith's avatar

@Artak

thankz for you suggestion.. i understood. but where i can add action listener.


<script type="text/javascript">
 $('#search_fromdate,#search_todate,#search_time').on('change',function(){

        $.ajax({
        dataType: 'json',
        type : 'get',
        url : '{{URL::to('search_excess_milk_details')}}',

        data:{ 'search_fromdate': $('#search_fromdate').val(),
                'search_todate': $('#search_todate').val(),
                'search_time': $('#search_time').val(),

        },

        success:function(data)
        {

            var res='';
            $.each (data, function (key, value) {
            res +=
            '<tr>'+
                '<td>'+value.date+'</td>'+
                '<td>'+value.time+'</td>'+
                '<td>'+value.no_of_litre+'</td>'+
                '<td>'+value.note+'</td>'+
                '<td>'+ "<a href='{{ route('Excess_milk.show',$ex->id) }}'  class='btn btn-info' style='width: 40%; float:left; margin-right:5px;'>Edit</a>"+

                "<a href='{{ route('Excess_milk.show',$ex->id) }}' class='btn btn-info' style='width: 30%; '>Delete</a>"


                +'</td>'+
            '</tr>';

   });
            $('tbody').html(res);
        }


    });
    })

this is my javascript in textbox change event

and this is my controller

$xcess_milks = Excess_milk::where(function ($query) use ($request)
                       {

                        if (!empty($request->search_fromdate) && !empty($request->search_todate) ) {

                            $query->whereBetween('date', [$request->search_fromdate, $request->search_todate]);
                          }                

                    })->orderBy('date','asc')->take(2)->get();


                    return $xcess_milks;

now what should i do?? kindly help..

Please or to participate in this conversation.