Monday, February 27, 2017

[SOLVED]Query Builder with multiple entry points

I have different filters I want to use for my model, but they are all optional.
if($foo) {
    $model = Model::where('id', 1);
}
if($bar) {
   $model = $model->where('age', 3);
}

So this code will only run if the first statement will success.
$model = Model::where('id', '<>', -1);
if($foo) {
    $model->where('id', 1);
}
if($bar) {
   $model->where('age', 3);
}

This would work, but it's dirty :(
So is it possible to save the Model to a variabel so I don't have to make a static call inside all if statements?



via mimo

Advertisement