1 回答

TA貢獻1834條經驗 獲得超8個贊
試試這個
例如,如果找到單詞,則使用類似搜索,然后第一個條件為真
如果用戶設置了開始日期和截止日期,則第二個條件為 true
如果用戶同時填寫(單詞和日期),則您的兩個條件都為真。
如果你沒有填寫任何值,那么只得到前10(十)個帖子(沒有條件匹配)
public function search(Request $request){
$posts = Post::query();
$from = $request->from;
$to = $request->to;
$word = trim($request->word);
if($word && !empty($word)){
$posts->where('title', 'LIKE', '%' . $word . '%')
->orWhere('content', 'LIKE', '%' . $word . '%')
->orWhere('subtitle', 'LIKE', '%' . $word . '%');
}
if(!empty($from) && !empty($to)){
$posts->whereBetween('created_at', [$from, $to]);
}
$searched = $posts->paginate(10);
return view('page.search', compact('searched', 'from', 'to'));
}
- 1 回答
- 0 關注
- 123 瀏覽
添加回答
舉報