title: how to implement Laravel AJAX live search on CRUD management table?
body: <p>I have a CRUD table that's holding records of doctors that get
  retrieved from the MySQL database. I've been following a few tutorials
  and managed to set up AJAX live results search.</p>
  <p>Now the problem comes from the fact that the logic is stored in a
  Controller. I can succesfully retrieve the details from the database
  using '-- foreach($data as $row) --' but I don't know how to retrieve
  the CRUD actions ( In this case  Delete.The create method is separated
  from the table itself. I also can't implement status cell using i)</p>
  <p>I've tried putting the actions inside the td'. .'td. tags but I get
  Routing errors.</p>
  <p>Any help is appreciated.</p>
  <p>I have the code for the CRUD actions, images etc..inside
  &quot;index.blade.php&quot;. They should be somehow implemented in the
  controller though.  doctor controller</p>
  <pre><code>  function liveSearchDoctor(Request $request)
          {
           if($request-&gt;ajax())
           {
            $output = '';
            $query = $request-&gt;get('query');
            
            if($query != '')
            {
              
              $data =Doctor::
                  where('name', 'like', '%'.$query.'%')
                  -&gt;orWhere('mobile', 'like', '%'.$query.'%')
                  -&gt;orWhere('email', 'like', '%'.$query.'%')
                  -&gt;orderBy('id', 'desc')
                  -&gt;paginate(2);
            
               
            }
            else
            {
              $data =Doctor::latest()-&gt;paginate(2);
            }
            $total_data = $data-&gt;count();
            if($total_data &gt; 0)
            {
              foreach($data as $row)
              {
                  $output .= '
                              &lt;tr&gt;
                                  &lt;td class=&quot;center&quot;&gt;&lt;img src=&quot;'.$row-&gt;image.'&quot; style=&quot;width:40px;height:40px;&quot;/&gt;&lt;/td&gt;
                                  &lt;td&gt;'.$row-&gt;id.'&lt;/td&gt;
                                  &lt;td&gt;'.$row-&gt;name.'&lt;/td&gt;
                                  &lt;td&gt;'.$row-&gt;department-&gt;name.'&lt;/td&gt;
                                  &lt;td&gt;'.$row-&gt;email.'&lt;/td&gt;
                                  &lt;td&gt;'.$row-&gt;mobile.'&lt;/td&gt;
                                  //status should be implement here 
                                  &lt;td&gt;&lt;a class=&quot;btn btn-primary&quot; href=&quot;'.route('doctors.edit',$row-&gt;id).'&quot;&gt;&lt;i class=&quot;fas fa-edit&quot;&gt;Edit&lt;/i&gt;&lt;/a&gt;
                                  //delete form should be implement here
      
                                  
                                  &lt;/td&gt;
                              &lt;/tr&gt;
                              ';
              }
      
      
            }
            else
            {
             $output = '
             &lt;tr&gt;
              &lt;td align=&quot;center&quot; colspan=&quot;5&quot;&gt;No Data Found&lt;/td&gt;
             &lt;/tr&gt;
             ';
            }
            $data = array(
             'table_data'  =&gt; $output,
             'total_data'  =&gt; $total_data
            );
      
            echo json_encode($data);
           }
          }
  </code></pre>
  <p>index.blade.php (( Note: The CRUD actions and everything is here
  implemented succesfully. I however can't implement delete in the
  controller )) here is the index.blade.php</p>
  <pre><code>@extends('layout.master')
  @section('css')
  
  @endsection
  @section('content')
  
  &lt;br&gt;
  &lt;div class=&quot;br-pagebody&quot;&gt;
     &lt;div class=&quot;br-section-wrapper&quot;&gt;
         @include('include._message')
     &lt;div class=&quot;row&quot;&gt;
         &lt;h6 class=&quot;br-section-label&quot;&gt;Doctors List&lt;/h6&gt;
     &lt;/div&gt;
       &lt;div class=&quot;row&quot;&gt;
           &lt;div class=&quot;col-lg-4 &quot;&gt;
            &lt;div class=&quot;form-group&quot;&gt;
              &lt;input type=&quot;text&quot; name=&quot;search&quot; id=&quot;search&quot; class=&quot;form-control&quot; placeholder=&quot;Search doctor Data&quot; /&gt;
             &lt;/div&gt;
           &lt;/div&gt;
  
  
  
           &lt;div class=&quot;col-lg-3 offset-lg-5&quot;&gt;
               &lt;a href=&quot;{{route('doctors.create')}}&quot; class=&quot;btn btn-primary&quot; style=&quot;margin-left: 35px&quot;&gt;Add New Doctor&lt;/a&gt;
           &lt;/div&gt;
       &lt;/div&gt;
  
     &lt;/br&gt;
       
     &lt;div class=&quot;table-responsive&quot;&gt;
      &lt;table class=&quot;table table-bordered table-colored table-dark&quot;&gt;
        &lt;thead class=&quot;thead-colored thead-dark&quot;&gt;
              &lt;tr&gt;
                  &lt;th&gt;Image&lt;/th&gt;
                  &lt;th&gt;Doctor Id&lt;/th&gt;
                  &lt;th&gt;Name&lt;/th&gt;
                  &lt;th&gt;Department&lt;/th&gt;
                  &lt;th&gt;Email&lt;/th&gt;
                  &lt;th&gt;mobile&lt;/th&gt;
                  &lt;th&gt;Status&lt;/th&gt;
                  &lt;th&gt;Actions&lt;/th&gt;
              &lt;/tr&gt;
  
  
          &lt;/thead&gt;   
       &lt;tbody&gt;   
          @foreach($doctors as $doctor)
            &lt;tr&gt;
              &lt;td class=&quot;center&quot;&gt;&lt;img src=&quot;{{URL::asset($doctor-&gt;image)}}&quot; style=&quot;width:40px;height:40px;&quot;/&gt;&lt;/td&gt;
                &lt;td class=&quot;center&quot;&gt;{{$doctor-&gt;id}}&lt;/td&gt;
                &lt;td class=&quot;center&quot;&gt;{{$doctor-&gt;name}}&lt;/td&gt;
                &lt;td class=&quot;center&quot;&gt;{{$doctor-&gt;department-&gt;name}}&lt;/td&gt;
                &lt;td class=&quot;center&quot;&gt;{{$doctor-&gt;email}}&lt;/td&gt;
                &lt;td class=&quot;center&quot;&gt;{{$doctor-&gt;mobile}}&lt;/td&gt;
                          
                &lt;td class=&quot;center&quot;&gt;
                    @if($doctor-&gt;status==1)
                        &lt;span class=&quot;badge badge-success&quot;&gt;Active&lt;/span&gt;
                    @else
                        &lt;span class=&quot;label label-danger&quot;&gt;Deactive&lt;/span&gt;
                    @endif
                    
                &lt;/td&gt;
                &lt;td class=&quot;center&quot;&gt;
                   
                    &lt;a class=&quot;btn btn-primary&quot; href=&quot;{{route('doctors.edit',$doctor-&gt;id)}}&quot;&gt;
                      &lt;i class=&quot;fas fa-edit&quot;&gt;Edit&lt;/i&gt; 
                    &lt;/a&gt;
                    
                    {!! Form::open(['method' =&gt; 'DELETE','route' =&gt; ['doctors.destroy', $doctor-&gt;id],'style'=&gt;'display:inline']) !!}
                    {!! Form::submit('Delete', ['class' =&gt; 'btn btn-danger']) !!}
                    {!! Form::close() !!}
                &lt;/td&gt;
            &lt;/tr&gt;
            @endforeach
          &lt;/tbody&gt;
      &lt;/table&gt;  
      &lt;/div&gt;&lt;!-- table-wrapper --&gt;
      &lt;div class=&quot;d-flex justify-content-center&quot;&gt;
        {!! $doctors-&gt;links() !!}
    &lt;/div&gt;
  
    &lt;/div&gt;
  &lt;/div&gt;
  
                        
          
  @endsection
  @section('js')
  &lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js&quot;&gt;&lt;/script&gt;
  &lt;script&gt;
  $(document).ready(function(){
  
   fetch_doctor_data();
  
   function fetch_doctor_data(query = '')
   {
    console.log(query)
    $.ajax({
     url:&quot;{{route('search.doctor')}}&quot;,
     method:&quot;GET&quot;,
     data:{'query':query},
     dataType:'json',
     success:function(data)
     {
       console.log(data)
      $('tbody').html(data.table_data);
     }
    })
   }
  
   $(document).on('keyup', '#search', function(){
    var query = $(this).val();
    fetch_doctor_data(query);
   });
  });
  &lt;/script&gt;
       
  @endsection
  </code></pre>