I have an overview site with a filter function on top. The user can filter for location and department.
The code of the controller looks like this:
public function index(Request $request)
{
$posts = Post::orderBy('titel')
->get();
$standorts = Standort::get();
$abteilungs = Abteilung::get();
if ($request->filled('s')) {
$query = strtolower($request->get('s'));
$posts = $posts->filter(function ($post) use ($query) {
if (Str::contains(strtolower($post->Titel), $query)) {
return true;
}
return false;
});
}
return view('posts.overview', ['posts' => $posts], ['standorts' => $standorts]);
}
I need to provide the $abteilungs = Abteilung::get(); aswell, but when I return it like this:
return view('posts.overview', ['posts' => $posts], ['standorts' => $standorts], ['abteilungs' => $abteilungs]);
The last part in the brackets is greyed out and I can't access it.
Is there a way to make this work or is the return view limited to two parameters?
$posts = Post::orderBy('titel')->when($request->filled('s'), fn ($q) => $q->where('titel', 'like', '%'.$request->s.'%'))->get();when()method executes the callback in second parameter only if the first parameter is true. laravel.com/docs/8.x/queries#conditional-clauses