大家好,最近我在Laravel7.x上启动了一个项目,但我遇到了一些问题。
我用laravel默认分页进行了搜索。
这是我的代码:
搜索表单
<form action="/search" method="GET">
<input type="text" name="k" id="k" value="" placeholder="Search...">
<button type="submit">Search</button>
</form>
控制器
public function get_search(Request $request)
{
$keyword = request('k');
$data = DB::table('posts')
->where('title', 'LIKE', '%$keyword%')
->orderBy('id', 'DESC')
->paginate(20);
return view('search', ['data' => $data]);
路线
Route::get('/search',['uses' => 'MainController@get_search', 'as' => 'search']);
查看
@foreach($data as $item)
{{ $item->title }}
@endforeach
{{ $data->links() }}
所以我得到了类似/search?key=keyword&page=2
的url
我只想让自己看起来像/search/keyword/page/2/
谢谢!
更换->paginate(20)
通过->paginate(20, ['*'], 'page', $page)
它没有记录在案。
我建议您修改routes/web.php文件。使用路由参数
Route::get('/search/{keyword}/page/{page}', 'SearchController@index')->name('.search');
然后,在控制器上,
public function index($keyword, $page){ //Receive parameters
$data = DB::table('posts')
->where('title', 'LIKE', '%$keyword%')
->orderBy('id', 'DESC')
->paginate(20);
return view('search', ['data' => $data]);
...
}
好的,我做了你上面说的,当在搜索刀片上获得print_r($data)
时,我得到了这个
Array
(
[current_page] => 1
[data] => Array
(
[0] => stdClass Object
(
[title] => keyword 1
)
[1] => stdClass Object
(
[title] => keyword 2
)
[2] => stdClass Object
(
[title] => keyword 3
)
[3] => stdClass Object
(
[title] => keyword 4
)
[4] => stdClass Object
(
[title] => keyword 5
)
[5] => stdClass Object
(
[title] => keyword 6
)
[6] => stdClass Object
(
[title] => keyword 7
)
[7] => stdClass Object
(
[title] => keyword 8
)
[8] => stdClass Object
(
[title] => keyword 9
)
[9] => stdClass Object
(
[title] => keyword 10
)
)
[first_page_url] => hxxp://xxxx.xxx/search/keyword?page=1
[from] => 1
[last_page] => 5
[last_page_url] => hxxp://xxxx.xxx/search/keyword?page=5
[next_page_url] => hxxp://xxxx.xxx/search/keyword?page=2
[path] => hxxp://xxxx.xxx/search/keyword
[per_page] => 10
[prev_page_url] =>
[to] => 10
[total] => 49
)
在我替换urlhxxp://xxxx.xxx/search/keyword/page/2/
时显示相同的结果
任何URL输出相同的结果,只是更改了?page=x,但是结果是相同的。基本上CCD_ 82/不起任何作用。
- 只有当我将url更改为
hxxp://xxxx.xxx/search/keyword/page/2/
时,结果才会更改?page=2我不需要