Reputation: 525
:)
I have implemented a search functionality for users who want to browse job postings, it looks like this:
https://i.sstatic.net/wGcW6.png
The code for it looks like this:
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]);
}
My issue is that only one parameter gets searched for. So when a user enters a search term "IT" + a location (Standort) or a department (Abteilung) only the search term is searched for.
I tried countless different things for 2 hours but I just cant get it to work. If you have any tipps let me know!
EDIT: when I leave out the orWhere('id', '=', $word)
it works, but I have to leave it in. But I cant change it to normal where()
because then it returns no matches.
Upvotes: 1
Views: 50
Reputation: 525
public function index(Request $request)
{
if ($word = $request->get('s')) {
if (is_numeric($word)) {
$postsQuery = $postsQuery
->where('id', '=', $word)
->where('isActive', 1)
->where('is_Canceled', 0);
} else {
$postsQuery = $postsQuery
->where('titel', 'like', "%{$word}%")
->where('isActive', 1)
->where('is_Canceled', 0);
}
}
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 is not an elegant solution but I just check if the search-term is a number or a word and then proceeds to the according condition and search function
Upvotes: 1
Reputation: 653
You did:
where a and b and c or d
But you want:
where (a and b and c) or d
https://laravel.com/docs/8.x/queries#logical-grouping
So roughly, you should strive to build something like this:
$posts = Post::query()
->where(function (Builder $query) {
$query->where('clauseA', '=', 1)
->where('clauseB', '=', 2)
->where('clauseC', '=', 3);
})
->orWhere('id', '=', $word)
->get();
Upvotes: 2