Skip to content

Commit 7da4845

Browse files
committed
allow to search for not existing related model using the ! prefix
1 parent cfd9209 commit 7da4845

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/Matthenning/EloquentApiFilter/EloquentApiFilter.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,16 @@ private function applyFilter(Builder $query, $field, $filter, $or = false)
184184
private function applyNestedFilter(Builder $query, array $fields, $operator, $value, $or = false)
185185
{
186186
$relation_name = implode('.', array_slice($fields, 0, count($fields) - 1));
187-
$relation_field = $fields[count($fields) - 1];
187+
$relation_field = end($fields);
188+
if ($relation_name[0] == '!') {
189+
$relation_name = substr($relation_name, 1, strlen($relation_name));
190+
191+
$that = $this;
192+
193+
return $query->whereHas($relation_name, function ($query) use ($relation_field, $operator, $value, $that, $or) {
194+
$query = $that->applyWhereClause($query, $relation_field, $operator, $value, $or);
195+
}, '=', 0);
196+
}
188197

189198
$that = $this;
190199

0 commit comments

Comments
 (0)