【发布时间】:2026-01-22 21:00:01
【问题描述】:
:)
我已经为想要浏览招聘信息的用户实现了一个搜索功能,它看起来像这样:
https://i.stack.imgur.com/wGcW6.png
它的代码如下所示:
public function index(Request $request)
{
if ($word = $request->get('s')) {
$postsQuery = $postsQuery
->where('titel', 'like', "%{$word}%")
->where('isActive', 1)
->where('is_Canceled', 0)
->orWhere('id', '=', $word);
}
if ($location = $request->get('standort') and $request->get('standort') != 'alle') {
$postsQuery = $postsQuery->where('Standort', $location);
}
if ($dep = $request->get('abteilung') and $request->get('abteilung') != 'alle') {
$postsQuery = $postsQuery->where('abteilung_name', $dep);
}
$posts = $postsQuery->orderBy('titel')->get();
if ($posts->count() == 0) {
return view('posts.overview', ['posts' => $posts, 'standorts' => $standorts, 'abteilungs' => $abteilungs])
->withErrors(['error' => 'Es wurde kein Treffer gefunden, versuchen Sie es mit anderen Suchparametern']);
}
return view('posts.overview', ['posts' => $posts, 'standorts' => $standorts, 'abteilungs' => $abteilungs]);
}
我的问题是只搜索一个参数。因此,当用户输入搜索词“IT”+ 位置 (Standort) 或部门 (Abteilung) 时,只会搜索搜索词。
我在 2 小时内尝试了无数不同的东西,但我就是无法让它发挥作用。如果您有任何提示,请告诉我!
编辑:当我忽略 orWhere('id', '=', $word) 时,它可以工作,但我必须将它留在里面。但我无法将其更改为正常的 where(),因为它不会返回任何匹配项。
【问题讨论】:
标签: php laravel search eloquent