So here is a peace of code without using when
if ($id != null) { $result->where('users.id', $id); } $result->get();
If you want to do this using when you do it this way
$result->when($id,function( $q, $id){ $q->where('users.id', $id); }); $result->get();
Hope this helped