Home > OS >  Laravel empty request after send ajax on keyup
Laravel empty request after send ajax on keyup

Time:06-30

I have empty request from ajax. In site request send this input value but controller not catching any request value my code:

public function searchUser(Request $request): JsonResponse
{
    $string = $request->input('searchUser');

    $user = DB::table('users')->where('name', 'LIKE','%'. $string.'%')
        ->orWhere('surname', 'LIKE','%'. $string.'%')
        ->orWhere('phone_number', 'LIKE','%'. $string.'%')
        ->orWhere('email', 'LIKE','%'. $string.'%')


        ->get();


    return response()->json([
        'user' => $user,
        'string' => $string,
        'request'=>$request
    ]);
}

https://pastebin.com/YqWkL7xP

CodePudding user response:

I have view your JavaScript in Pastebin

$.ajax({
.
.
.
  data: {
     'searchUser': textFieldValue
  },
.
.
.
});

I think the variable name in the data you want to pass into controller should same with controller

Controller.php

$string = $request->input('searchUser');
.
.
.

You also can change the variable name searchUser be string too in the controller, It must be same.

Hope it will help.

  • Related