anditsung's avatar

laravel nova belongsto filter

i have post model with belongsto user some of the user is not active how to show only active user on belongsto?

0 likes
5 replies
anditsung's avatar

i got the answer for this.. not well documented on the documentation

Resource

public function fields(Request $request)
    {
        return [

            BelongsTo::make('Formula')
                ->searchable()
                ->rules('required'),

            BelongsTo::make('Tank')
                ->rules('required'),
        ];
    }

Model

public function formula() : BelongsTo
    {
        return $this->belongsTo(Formula::class);
    }

    public function tank()
    {
        return $this->belongsTo(Tank::class);
    }

to filter the relation must add this code on the Resource

public static function relatableFormulas(NovaRequest $request, $query)
    {
        return $query->where('active', true);
    }

    public static function relatableTanks(NovaRequest $request, $query)
    {
        return $query->where('active', true);
    }

if the name for the field is Something then the function name must be relatableSomethings

7 likes
altos's avatar

this is huge, where did you even find this?

1 like
RhysLees's avatar

How would i filter based from another fields value?

Please or to participate in this conversation.