Level 53
- Move out the query logic out of the controller and put it inside your model(s) or create a different class that will handle your read / search operations .
- Create some query scopes
- I see a lot of if statements which duplicate but they have only a few different things. These can be put inside one method inside a model class which by passing stuff as parameters, the query where methods will be shortened.
- The lines where you get the parameters from the search query string can be cut down by replacing these 3 lines with a helper function which checks for request input.
// searchBy() helper or figure out a better name for it.
public function searchBy($value, $query, $field, $type) {
if($value)
{
return $query->where($field, $type, $value);
}
}
// hasSearchParameter
public function hasSearchParameter($value, $default)
{
if($value)
{
return $value;
}
return $default;
}