0

I am new to the Laravel Search functionality and pagination. I am using Laravel 5.6 and have search functionality.

Controller Code

public function search()
    {
    $users = User::whereRole_id(1)->where(function($query) {
                $q = Input::get ( 'q' );
                return $query->where ( 'name', 'LIKE', '%' . $q . '%' )->orWhere ( 'email', 'LIKE', '%' . $q . '%' )->orWhere ( 'phone', 'LIKE', '%' . $q . '%');
        })->paginate(8)->setPath('');
        $pagination = $users->appends ( array ('q' => Input::get ( 'q' ) ) );
                if (count($users) > 0){
                    return view( 'admin.users.index',compact('users'));
                }else{
                    $q = Input::get ( 'q' );
                    Session::flash('Nosearch',"No Details found with $q .Try to search again !");
                    return view( 'admin.users.index',compact('users'));
            }
    }

My View Code:

I am using {{$users->links()}} to display the links and when I do view source code it is giving correct param as well as page numbers like this:

<li class="page-item active" aria-current="page"><span class="page-link">1</span</li><li class="page-item"><a class="page-link" href="?q=abc&amp;page=2">2</a></li>

Route Code:

Route::group([
    'as'=>'admin.',
    'prefix' => 'admin',
    'middleware' => 'admin'
], function() {
    Route::post('/users/search', 'AdminUsersController@search');
}

When I click on next page it gives me error as below Page Not Found Error

Kindly, help me where am I going wrong please? Thanks, Palak

Palak
  • 15
  • 6
  • When you are encountered with page not found, see the browser's address bar what does it show? – Ruman Jun 20 '18 at 04:51
  • Ruman - Browser bar shows this link "http://ssapp/admin/users/search?q=abc&page=2" which looks correct to me. – Palak Jun 20 '18 at 05:03
  • Please show your other routes in order as they appear in your routes file. – Ruman Jun 20 '18 at 05:12
  • Sorry, here are my routes please: Route::group([ 'as'=>'admin.', 'prefix' => 'admin', 'middleware' => 'admin' ], function() { Route::resource('users', 'AdminUsersController'); Route::post('/users/search', 'AdminUsersController@search'); Route::get('/users/export/{type}', 'AdminUsersController@export'); Route::get('transactions/index', 'AdminTransactionsController@index')->name('transactions.index'); Route::post('/transactions/search', 'AdminTransactionsController@search'); Route::get('/transactions/export/{type}', 'AdminTransactionsController@export'); }); – Palak Jun 23 '18 at 09:51
  • Ok! I'll update my answer. – Ruman Jun 23 '18 at 09:53

2 Answers2

0

Move your resource route to the end. You are getting a 404 because, your uri users/search matches the show resource route i.e users/{user} where laravel tries to find a user with user id "search" which is not found so it gives a ModelNotFoundException with a 404.

You should re-order your routes and also make your search route a GET route, otherwise next page links will always hit the uri users/{user} with GET request method, which will again result in the situation explained above.

Route::group([
    'as'=>'admin.',
    'prefix' => 'admin',
    'middleware' => 'admin'
], function() {
//Note that it is now a get method
    Route::get('/users/search', 'AdminUsersController@search');
    Route::get('/users/export/{type}', 'AdminUsersController@export');
//Moved your resource route to the end of `users` uri. 
Route::resource('users', 'AdminUsersController');
 Route::get('transactions/index', 'AdminTransactionsController@index')->name('transactions.index');
 Route::post('/transactions/search', 'AdminTransactionsController@search');
 Route::get('/transactions/export/{type}', 'AdminTransactionsController@export');

});
Ruman
  • 191
  • 1
  • 8
  • Ruman, thanks for the reply. I did this but still it is giving me same error. – Palak Jun 24 '18 at 11:16
  • Oh.. you know what you should use `get`. Because when click on paginator's next button its a get request. Also, it is suggested to use get request only for searching – Ruman Jun 24 '18 at 11:19
  • Awesome! Thanks so much Ruman. Its solved now. You made my day!!! Thanks so much! I was struggling for 2 days with this issue. – Palak Jun 24 '18 at 11:23
  • Do I need to close this question anywhere on the stack overflow? Please advise I am fairly new to using this site. – Palak Jun 24 '18 at 11:25
  • No you dont. If the solution worked you mark the answer as selected. And you're done. – Ruman Jun 24 '18 at 11:51
0
Route::group([
    'as'=>'admin.',
    'prefix' => 'admin',
    'middleware' => 'admin'
], function() {
    Route::post('/users/search', 'AdminUsersController@search');
    Route::get('/users/export/{type}', 'AdminUsersController@export');
    Route::resource('users', 'AdminUsersController');
    Route::get('transactions/index', 'AdminTransactionsController@index')->name('transactions.index');
    Route::post('/transactions/search', 'AdminTransactionsController@search');
    Route::get('/transactions/export/{type}', 'AdminTransactionsController@export');
});

I have pasted the image for reference I am going to this url : http://ssapp/admin/users/search?q=user1&page=2

Regards, Palak

Palak
  • 15
  • 6