Level 75
Sep 6, 2021
4
Level 1
Multiple condition for query
Hi,
Suppose I have a table called Book and I'd like to allows the users to filter the content based on:
startWith (first letter of the book)
Genre
sortOrder(recently added,most popular(view count), releasedate,rating...)
searchString (where title = %user_string%)
What is the best way to build this query without so many if/else
Level 75
@jsvaryuna there is nothing wrong with if's, just my opinion.
Just example
$query = Dog::where('dogname', 'like', $dogsch);
if ($aval == "n") {
$query->where('adopted', '=', 1);
} else if ($aval == "y") {
$query->where('adopted', '=', 0);
}
$dogs = $query->orderBy('lastedit', 'DESC')->paginate(5);
$params = array('psch' => $dogsearch, 'aval' => $aval);
etc
2 likes
Please or to participate in this conversation.