public function search_page(Request $request){ $key = $request->input('query'); $news = []; $total = 0; $query = ''; if ($key){ $news = News::query()->where('news_title','like',"%$key%")->orderBy('created_at','desc')->paginate(15,['*'],'search-page')->withQueryString(); $total = $news->total(); $query = $key; } return view('home.search.list',compact('news','total','query')); }写注释
时间: 2024-01-13 12:05:41 浏览: 102
public_api_request:公共API请求
/**
* 搜索页面
*
* @param Request $request 请求对象
* @return \Illuminate\View\View 返回搜索结果页面视图
*/
public function search_page(Request $request){
$key = $request->input('query');
$news = [];
$total = 0;
$query = '';
// 如果关键词存在
if ($key){
// 根据新闻标题模糊查询新闻
$news = News::query()->where('news_title','like',"%$key%")->orderBy('created_at','desc')->paginate(15,['*'],'search-page')->withQueryString();
// 获取查询到的新闻总数
$total = $news->total();
// 保存查询关键词
$query = $key;
}
// 返回搜索结果页面视图,并传递相关数据
return view('home.search.list',compact('news','total','query'));
}
阅读全文