I am using the Spatie/laraver-query-builder package to make some filters in my application.
I have a hasMany relation that looks like this:
ImageRequest Model:
public function requestTypes()
{
return $this->belongsToMany('App\RequestType');
}
RequestType Model:
public function imageRequests()
{
return $this->hasMany('App\ImageRequest');
}
I have a table image_requests
and table request_types
and inbetween a pivot table image_request_request_type
.
request_types table
pivot table
Now I am trying to filter my image_request rows by the type
field in my request_types table like this:
private $allowedFilters = [
'id',
'user_id',
'pv_number',
'begin_date_of_facts',
'end_date_of_facts',
'begin_hour_of_facts',
'end_hour_of_facts',
'location_of_facts',
'attachment',
'description',
'status',
'comment',
];
//
$query = QueryBuilder::for(ImageRequest::class)
->with(['requestTypes'])
->allowedIncludes('requestTypes')
->allowedFilters($this->allowedFilters, (Filter::custom('type', RequestTypeFilter::class)));
My RequestTypeFilter (based on spatie/laravel-query-builder documentation):
public function __invoke(Builder $query, $value, string $property) : Builder
{
return $query->whereHas('requesttypes', function (Builder $query) use ($value) {
$query->where('type', $value);
});
}
My error message when trying to filter not even on type but on other field in image_requests table: (http://127.0.0.1:8000/?filter[pv_number]=651&filter[type]=)
) when I remove the type filter it works.
(1/1) InvalidFilterQuery Given filter(s)
type
are not allowed. Allowed filter(s) areid, user_id, pv_number, begin_date_of_facts, end_date_of_facts, begin_hour_of_facts, end_hour_of_facts, location_of_facts, attachment, description, status, comment
.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire