Filter data through AJAX with POST request Laravel 5.4

0

Goodnight friends.

I have a problem with the laravel framework and I can not manage to perform a simple task.

When I write in an input I automatically send via AJAX by POST the value of the input to filter my table of users.

$("#cedula").keyup(function(){

    var cedula = parseInt($(this).val());

    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

    $.ajax({
            type: 'POST',
            url: '{{ URL::route('traerNombre') }}',
            dataType : 'json',
            data: {
                documento: cedula   
            },
        success: function(result){
        //var arrData = JSON.parse(data);
        console.log(result);
        //$("#usuario").val(nombre);
        },
        error: function (data) {
        console.log('Error:', data);
        }});
    });

As you can see I put the token and everything is fine since when I test from the controller I return what I receive correctly.

Here is my custom method from the model (document is my column where I keep the identification number of my users):

public function scopeCedula($query, $cedula)
{
    return $query->where('documento', 'LIKE' ,'%$cedula%');
}

And here is my controller:

public function cedula(Request $request)
{
    $cedula = $request->documento;    
    $user   =  User::cedula($cedula);

    return response()->json($user);
}

It returns an error 500.

But if I do it this way:

    public function cedula(Request $request)
{
    $cedula = $request->documento;    
    $user   =  User::all();

    return response()->json($user);
}

if you return the full collection of the users of the table.

Please, I need your help, thank you.

    
asked by Cleibert Mora 10.06.2018 в 06:13
source

0 answers