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

AbdulBazith's avatar

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

Guys I am working in a project milkfarm

Thisis my view.blade image before search

Refer: https://imgur.com/a/6gSd7rd

My view.blade


 <thead>

              <th>Date</th>
              <th>Time</th>
              <th>No of Litres (Lt)</th>
              <th>Note</th>
              <th>Action</th>

</thead>


      <tbody >
            @foreach($exs as $ex)
                <tr>


                    <td>{{ $ex->date->format('d-m-Y')  }}</td>
                    <td>{{ $ex->time }}</td>
                    <td>{{ $ex->no_of_litre }}</td>
                    <td>{{ $ex->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>


  {!! Form::open(['route'=>['Excess_milk.destroy',$ex->id], 'onsubmit' => 'return ConfirmDelete()','method'=>'Delete']) !!}


        {{ Form::submit('Delete',array('class'=>'btn btn-info','style'=>'width: 50%; ' )) }}
    </td>
             {!! Form::close() !!}
                </tr>
            @endforeach          
            <tr ><td colspan='2' align="center"><b> Total</b></td>
                <td ><b>{{ $grandTotal}}</td>
                <td colspan='2'></td>
             </tr>
            
      </tbody>

      </table>

            <div class="text-center">

                {!! $exs->Links(); !!}
            </div>


Pasted the center portion result and buttons only

See the image I have search box in the top and also two buttons PDF and EXCEL

This is after search returned from ajax. I searched time=”am” this displayed using ajax success

Refer: https://imgur.com/a/SwXPljy

https://i.imgur.com/GAD8Uci.png

See even the total is not displayed after the search, becoz don’t know how to return the sum(fields) in ajax.

Normally my project runs. I have edit and delete button in my view.blade. So when I click edit button it takes the specific id and its data to my edit() in controller

I have delete button when I click the delete button it asks the user a

confirmation and then if user selects yes then it specifically picks the

record based on the id and moves to the controller destroy function. I have included my delete in form tag

This is the javascript code for delete confirmation


 function ConfirmDelete()
    {
    var x = confirm("Are you sure you want to delete?");
    if (x)
      return true;
    else
      return false;
    }

Using ajax call I retrived the information for search. Worked successfully. But I have edit and delete button. After ajax search I displayed the records in ajax success. So I need to place the edit and delete button in the ajax success also.

this is my ajax which moves to the search_excess_milk_details


$('#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.edit',$ex->id) }}'  class='btn btn-info' style='width: 40%; float:left; margin-right:5px;'>Edit</a>"+

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


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

   });

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


    });
    })

my controller for search function search_excess_milk_details


 public function search_excess_milk_details( Request $request )
    {

                    $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]);
                            }

                          if (!empty($request->search_time)) 
           {
                            $query->Where('time', 'LIKE', '%' . $request->search_time . '%');
                          }

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

                    return $xcess_milks;
    }

I am having three doubts. Need solution

I used date format in my model and view.blade So normally the date format works. But after ajax search the date format time is displayed. In ajax where I should I include the

format('d-m-Y')

My model


class Excess_milk extends Model
{

    public $table= 'excess_milk';

    protected $dates = [
        'created_at',
        'updated_at',
        'date'
    ];
}

My next doubt I used pagination in view.blade so before search the pagination(5) works correctly.

But after ajax search I used get(). If I give pagination then where how I should write the ajax success.

But after ajax search I used get(). If I give pagination then where how I should write the ajax success.

My third doubt is the buttons edit and delete.

How can I use the delete and edit button in ajax work same as view.blade work.

fourth doubt how could i pass the sum('fields') from controller to ajax success

during search the results are coming from ajax so I don’t know where I should use pagination ? where to use the date format and how to use the delete and edit button in ajax return

i think understood my question.

Kindly some one suggest please

Help me to solve my problem

0 likes
0 replies

Please or to participate in this conversation.