Possible to use double where clause in typeahead autocomplete?

Posted in CategoryLaravel
  • A
    ADB 1 month ago

    Hello, I'm coming from this post:
    https://itsolutionstuff.com/post/laravel-57-autocomplete-search-from-database-using-typeahead-jsexample.html

    I was told to ask for help here:

    I've changed the function to:
    ```

    public function autocomplete(Request $request)
    {
    $user_id = Auth::user()->id;
    $data = Pigeon::select("name")
    ->where([
    "name","LIKE","%{$request->input('query')}%",
    "user_id", "=", $user
    ])
    ->get();

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

    ```

    But this doesn't seem to work..

    I want to only show records where the user_id is the same as the current logged in users, so that they can only search for they're own records.

  • A
    ADB 1 month ago

    Got myself an answer of the laracast forums:

    This is how they told me to do it, and it works, for anyone wondering!

    public function autocomplete(Request $request)
    {
    $user = Auth()->user();

    $query = $request->input('query');
    $data = $user->pigeons()->where('name', 'LIKE' , '%' . $query . '%')->get();

    return $data->pluck('name');
    }

Please login or register to leave a response.

* You May Also Like Bellow Issue to check *