简体   繁体   中英

Too few arguments Laravel Nova relatable query with fields

I have a relationship that needs to be filtered differently depending on the Laravel Nova field it is being used to populate.

A single Locations has one or more Locations attached as options 
(BelongsToMany)
A single Process_type also has a single Locations set as a default 
(BelongsTo)

I'm using a relatable query to filter the available options for each of these two fields.

The field in my Location resource is as follows:

BelongsToMany::make('Neighborhood Locations', 'relatedLocations', Location::class)
                ->searchable()

The relatable query looks like this:

public static function relatableLocations(NovaRequest $request, $query, Field $field)
{
    if ($field instanceof BelongsToMany) {
        return $query->where('location_type_id', '=',8);
    }
    return $query;
}

This pattern came from Laravel Nova documentation on Dynamic Relatable Models ( https://nova.laravel.com/docs/3.0/resources/authorization.html#dynamic-relatable-methods ). It describes adding the field as a third parameter to the relatable query, then using it in a conditional to choose filter logic.

The error I'm getting is:

Too few arguments to function AppNovaProcess_type::relatableLocations(), 
2 passed and exactly 3 expected

My make() calls have three parameters as per the documentation, any idea what I'm missing?

I'm on Laravel 7.x and Nova 3.x

Source: Laravel

There should not be 4th argument in relatableLocations. So, remove Field $field from function.

Checkout relatable filtering from Nova documentation for more info:

Relatable Filtering

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM