0

I cannot search using ajax, that is my problem. I need to search without refreshing.

Here is my controller code; am I missing something here?

public function search(Request $request){
    if($request->ajax())
    {

        $employees = DB::table('employeefms')->where('last_name','LIKE','%'.$request->search.'%')
                                             ->orWhere('first_name','LIKE','%'.$request->search.'%')->get();

        return response();
    }
}

Here is my view. I know I have a lot of fields in here! Please check if I'm missing something:

     @foreach ($employees as $employee)
    <tbody>
      <tr>
        <td>{{ $employee->employee_no}}</td>
        <td>{{ $employee->last_name}}</td>
        <td>{{ $employee->first_name}}</td>
        <td>{{ $employee->middle_name}}</td>
        <td>{{ $employee->nick_name}}</td>
        <td>{{ $employee->gender}}</td>
        <td>{{ $employee->birthdate }}</td>
        <td>{{ $employee->age}}</td>
        <td>{{ $employee->birthplace}}</td>
        <td>{{ $employee->province}}</td>
        <td>{{ $employee->doMarriage}}</td>
        <td>{{ $employee->height}}</td>
        <td>{{ $employee->weight}}</td>
        <td>{{ $employee->bloodtype}}</td>
        <td>{{ $employee->nationality }}</td>
        <td>{{ $employee->religion}}</td>
        <td>{{ $employee->civil_stats}}</td>
        <td>{{ $employee->sss_no}}</td>
        <td>{{ $employee->tin_id}}</td>
        <td>{{ $employee->phil_no}}</td>
        <td>{{ $employee->pagibig_no}}</td>
        <td>{{ $employee->address_no}}</td>
        <td>{{ $employee->street_no}}</td>
        <td>{{ $employee->brgy}}</td>
        <td>{{ $employee->municipality}}</td>
        <td>{{ $employee->cur_province}}</td>
        <td>{{ $employee->region}}</td>
        <td>{{ $employee->zipcode}}</td>
        <td>{{ $employee->per_address_no}}</td>
        <td>{{ $employee->per_street_no}}</td>
        <td>{{ $employee->per_brgy}}</td>
        <td>{{ $employee->per_municipality}}</td>
        <td>{{ $employee->per_province}}</td>
        <td>{{ $employee->per_region}}</td>
        <td>{{ $employee->per_zipcode}}</td>
        <td>{{ $employee->mobile_no}}</td>
        <td>{{ $employee->tel_no}}</td>
        <td>{{ $employee->email_ad}}</td>
        <td>{{ $employee->guard_name}}</td>
        <td>{{ $employee->guard_add}}</td>
        <td>{{ $employee->guard_relat}}</td>
        <td>{{ $employee->grd_mobile_no}}</td>

        <td><a href="/admin/employeemaintenance/{{ $employee->id }}/edit" class="btn btn-primary btn-sm"><i class="fa fa-edit"></i></a></td>
        <td>

                {!!Form::open(['action'=>['Admin\EmployeeFilemController@destroy', $employee->id],'method'=>'POST', 'align'=>'right'])!!}
                {{Form::hidden('_method', 'DELETE')}}
                {{Form::button('<i class="fa fa-trash"></i>',['type' => 'submit','class' => 'btn btn-sm btn-danger'])}}
                {!!Form::close()!!}

        </td>
      </tr>
    </tbody>       
      @endforeach

My AJAX:

 <script type="text/javascript">

$('#search').on('keyup',function(){
    $value=$(this).val();
        $.ajax({
        type : 'get',
        url  : '{{ URL::to('admin/employeemaintenance/search') }}',
        data : {'search':$value},
        success:function(data){
            var data1 = jQuery.parseJSON(data); 
            if(data1.msg == "success"){
                $.each(eval(data1.data), function(){
                    $('tbody').html(data);
                    })
                },
                //no data found
        }
    });
 })

</script>
halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

0

You have to make two improvements in your code as

1) either you need to turn off the CSRF protection for your route or pass "_token": "{{ csrf_token() }}" parameter with your data

2) you are returning response(), which doesn't make sense return $employees instead of that

RAUSHAN KUMAR
  • 5,846
  • 4
  • 34
  • 70
  • How to turn off csrf token? –  Dec 06 '18 at 15:20
  • 1
    It is more secure to use csrf tokens. You can add the following to the head of master.blade.php `` Instead of `return response()` You should use `return Response::json($employees);` – code9monkey Dec 06 '18 at 15:46
  • @SummerWinter to turn off the CSRF for all routes you need to comment it in the `kernel.php` or to turn off the CSRF for a particular route you need to add that route in the except array in `App\Http\Middleware\VerifyCsrfToken.php` – RAUSHAN KUMAR Dec 07 '18 at 08:14