0

我是 Laravel 搜索功能和分页的新手。我正在使用 Laravel 5.6 并具有搜索功能。

控制器代码

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'));
            }
    }

我的查看代码:

我正在使用 {{$users->links()}} 来显示链接,当我查看源代码时,它会给出正确的参数以及这样的页码:

<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::group([
    'as'=>'admin.',
    'prefix' => 'admin',
    'middleware' => 'admin'
], function() {
    Route::post('/users/search', 'AdminUsersController@search');
}

当我点击下一页时,它给了我如下错误 页面未找到错误

请帮助我,我哪里出错了?谢谢, 帕拉克

4

2 回答 2

0

将您的资源路线移动到最后。您得到 404 是因为,您的 uriusers/searchshow资源路径匹配,即users/{user}laravel 尝试查找用户 id 为“search”的用户,但未找到,因此它给出了ModelNotFoundExceptiona 404

您应该重新排序您的路线,并将您的搜索路线设为GET路线,否则下一页链接将始终users/{user}使用GETrequest 方法访问 uri,这将再次导致上述情况。

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');

});
于 2018-06-23T10:01:14.863 回答
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');
});

我已经粘贴了图片以供参考 我要去这个网址:http://ssapp/admin/users/search?q=user1&page=2

问候, 帕拉克

于 2018-06-24T11:21:06.283 回答