It seems like you're trying to apply a filter using Laravel's Eloquent query builder. The issue might be with how the leftJoin is being used within the filter's query closure. When you want to filter based on a condition in a joined table, you should ensure that the join is applied correctly and that the where condition is applied to the main query, not just within the join closure.
Here's a revised version of your filter:
Filter::make('Has guests')
->query(function (Builder $query): Builder {
return $query->leftJoin('saldi', 'hosts.id', '=', 'saldi.host')
->where('saldi.assigned', '>', 2);
}),
In this revised version, the where condition is applied to the main query builder instance, not within the leftJoin closure. This ensures that the filter condition (saldi.assigned > 2) is applied to the result of the join, not just to the join condition itself.
Also, make sure that the assigned column in the saldi table is not nullable or doesn't contain null values, as this could affect the results when using a leftJoin. If assigned can be null and you want to exclude those records, you might need to add an additional condition to filter out null values:
Filter::make('Has guests')
->query(function (Builder $query): Builder {
return $query->leftJoin('saldi', 'hosts.id', '=', 'saldi.host')
->where('saldi.assigned', '>', 2)
->whereNotNull('saldi.assigned');
}),
If you want to ensure that only hosts with a corresponding record in the saldi table are included, you might want to use an innerJoin instead of a leftJoin:
Filter::make('Has guests')
->query(function (Builder $query): Builder {
return $query->join('saldi', 'hosts.id', '=', 'saldi.host')
->where('saldi.assigned', '>', 2);
}),
This will only include rows where there is a matching record in both hosts and saldi tables, and where the assigned value is greater than 2.