laravel / nova-issues

556 stars 34 forks source link

searchable/filterable method with parameter does not work on relation field in filters #6385

Closed webard closed 6 months ago

webard commented 6 months ago

Description:

I have field named "Owner" which is simply User:

BelongsTo::make(___('Owner'), 'owner', User::class)
                ->filterable(function(NovaRequest $request, Builder $query, string $value) {
                    return $query->where('is_owner', true);
                })

but query from parameter seems to not work during searching in filter:

image

It seems to query data basing on $search property in User resource, but does not respect extension of builder from closure.

Generated query is:

select * from `users` where (`users`.`id` like '%admin%' or `users`.`fullname` like '%admin%' or `users`.`email` like '%admin%' or `users`.`phone` like '%admin%') and `users`.`deleted_at` is null order by `updated_at` desc

but should be

select * from `users` where (`users`.`id` like '%admin%' or `users`.`fullname` like '%admin%' or `users`.`email` like '%admin%' or `users`.`phone` like '%admin%') and `users`.`is_owner` = 1 and `users`.`deleted_at` is null order by `updated_at` desc   

Detailed steps to reproduce the issue on a fresh Nova installation:

  1. Configure filterable relation field and pass in argument closure with additional builder conditions
crynobone commented 6 months ago

The query you shown above are for searching the dropdown, while

->filterable(function(NovaRequest $request, Builder $query, string $value) {
    return $query->where('is_owner', true);
})

will be only be applied to the current resource SQL. Such as

SELECT * FROM `posts` WHERE `is_owner`=1

instead of the default

SELECT * FROM `posts` WHERE `user_id`=?
webard commented 6 months ago

@crynobone I know that. Why you closed issue without even identyfing use cases?

When we search relation from Edit view, in request data there is info about component, relationship etc. so we can use it to manage query in indexQuery() method.

When searching in filters, there is just /nova-api/resource/search without any additional data like resource, filter name etc. It will be enough to manage search query and it is not breaking change, just couple additional parameters in URL during filter searching.

This change cannot be planned?

crynobone commented 6 months ago

From the reply this is a duplicate of https://github.com/laravel/nova-issues/discussions/5097