简体   繁体   中英

search keyword in table list

I want to search keyword base on the word on table but when I search there is no output and no error display but the url display the keyword for example http://127.0.0.1:8000/users?keyword=sulaimani

Index.blade.php

<form action="{{ route('users.search') }}" method="GET">
    <input type="text" name="keyword" class="form-control" placeholder="Search for...">
          <button type="submit" class="btn btn-info">
             <i class="fa fa-search"></i> Search
          </button>
</form>

UserController.php

public function search(Request $request)
{
    $keyword = $request->get('keyword');
    $users = User::where('name', 'LIKE', '%' . $keyword . '%')->orwhere('email', 'LIKE', '%' . $keyword . '%')->paginate(4); 
    return view('users.index')->with(compact('users'));
}

web.php

Route::get('user/search', 'UserController@search')->name('users.search');

First try to find out the name or email like "sulaimani" in your users table. If they are returning what you need then come to the code. Just dump and die the variable $keyword and see what your are getting (dd($keyword))

Or you can just change the same like:

$keyword = $request->keyword;

Now in dump and die (dd($keyword)) if you are getting your input then just remove dd and hit the following query using eloquent,

$users = User::where('name', 'LIKE', "%{$keyword}%")->OrWhere('email', 'LIKE', "%{$keyword}%")->paginate(4);

Hope this may help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM