I'm currently trying to implement an issue logging service, when a task is completed a date can be entered into the 'completed' column which I have a CRUD for.
The idea is that the user selects from a dropdown with the following values:
- 0 - Hide completed tasks
- 1 - Show only completed tasks
- 2 - Show both completed and in progress tasks
My Controller code:
public function all(Request $request){
$result = IssueLog::where('deleted', '=', null)
->when($request->sys, function ($q) use($request){
$q->where('system', $request->sys);
})
->when($request->sub, function ($qw) use($request){
$qw->where('sub_system', $request->sub);
})
->when($request->complete, function ($qr) use($request){
switch ($request->complete) {
case 0:
return $qr->whereNull('completed');
case 1:
return $qr->whereNotNull('completed');
case 2:
return $qr->whereNull('completed')
->orWhereNotNull('completed');
default:
return $qr->whereNull('completed');
}
})
->orderByRaw('updated_at DESC NULLS LAST')
->get();
return $result;
}
Results from case 1 come back as expected however, case 0 and 2 seem to be exactly the same, where the dates that aren't null come through in case 0 where they shouldn't be. I've also made sure that before the function is run the correct value is passed into the request.
As you have a default clause in your inner
switch
control, I'm assuming that you want a fallback in case where thecompleted
it not provided in the request.Using
when()
means that you will conditionally apply a specific query. The first argument determines if it should be used or not - based on the "truthfulness" of the argument.0
is a falsy value, so it will not run when that's the case (have a look at the official manual on booleans).Instead, we can apply a
where()
clause with a closure. We can also clean it up, and re-use the same results for different conditions (such as the default andcase 0
). Forcase 2
, you want to return both those that are completed, and those that aren't - so instead of applyingwhereNull('completed')->orWhereNotNull('completed')
, you can just return the query itself.I've also replaced
where('deleted', '=', null)
withwhereNull('deleted')
, as that is the correct approach to check if a column isNULL
.You should also consider using
$request->has("sys")
(or something like!empty($request->sys)
) instead of supplying the actual request value to the first argument of yourwhen()
methods. These two functions return a boolean value, and is more explicit than passing the entire value.