Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

multiple field query filtering in Laravel

public function show(Request $request)
{
    $annonces = Annonce::when($request->term, function ($query, $term) {
        return $query->where('title', 'like', "%{$term}%");
    })->when($request->categorie, function ($query, $categorie) {
        return $query->where('categorie', 'like', "%{$categorie}%");
    })->when($request->price && in_array($request->price, ['more-expensive', 'less-expensive']), function ($query) use ($request) {
        return $query->orderBy('price', $request->price == 'less-expensive' ? 'asc' : 'desc');
    }, function ($query) {
        return $query->orderByDesc('id');
    })->paginate(15);

    return view('welcome', compact('annonces'));
}
Source by laracasts.com #
 
PREVIOUS NEXT
Tagged: #multiple #field #query #filtering #Laravel
ADD COMMENT
Topic
Name
7+5 =