John Davidson

php - Search functionality only uses one parameter

0 comments
Message:


:)


I have implemented a search functionality for users who want to browse job postings, it looks like this:


https://i.stack.imgur.com/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.



My Answer:
>input('search')) {
$jobs = Job::where('title', 'like', '%' . $word . '%')->paginate(10);
} else {
$jobs = Job::paginate(10);
}

return view('jobs.index', compact('jobs'));
}_

This code snippet only allows users to search for job postings based on the job title. If you want to allow users to search based on other parameters such as job description, location, or company name, you can modify the code to include those parameters in the search query.

For example, you can add additional conditions to the `where` clause to search for job postings based on multiple parameters:


if ($word = $request->input('search')) {
$jobs = Job::where('title', 'like', '%' . $word . '%')
->orWhere('description', 'like', '%' . $word . '%')
->orWhere('location', 'like', '%' . $word . '%')
->orWhere('company_name', 'like', '%' . $word . '%')
->paginate(10);
} else {
$jobs = Job::paginate(10);
}


This code snippet will allow users to search for job postings based on the job title, description, location, or company name. You can customize the search functionality further by adding more parameters or conditions to the search query as needed.

Rate this post

4 of 5 based on 6694 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us