Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Hala's avatar
Level 3

override nova resource

I have Food Model

the food has 4 type in database

in nova i make for each type a resource

so i have Class cuisine Food , Class holy_meal Food and so on

when i create holy meal the type fill in db as cuisine

how could i have same type as his class name

0 likes
3 replies
bobbybouwmann's avatar

Your question is a bit vague but I think you want to reuse the same model for multiple resources in Nova, right?

So in that case, you can create a new resource, point to the same model, but update the indexQuery to fetch the correct results

class HolyMeal extends Resource
{
    public static $model = \App\Food::class;

    public static function indexQuery(NovaRequest $request, $query): Builder
    {
        $query = $query->where('type', 'holy_meal');

        return parent::indexQuery($request, $query);
    }
}

Another solution is creating multiple models and using a global query scope to determine the correct type. You can create multiple models that look at the same table.

1 like
Hala's avatar
Level 3

@bobbybouwmann

Sorry if my explanation was not that clear

in food model i have two columns one is the name and other is type

i have 4 types each type is a resource in nova and it's work pretty , and i was having index query method as yours

but the problem is when i create Holy meal record , in the db the creation save the type as cuisine not the correct type , so how could i make it save type as the name of resource

piljac1's avatar

On top of my head I see 3 possible solutions :

  • Use the Nova Hidden Field package to set an hidden input containing your type value

  • Create your own custom field which implements a hidden input

  • Last resort: Use a (nasty) little hack to inject your type in the request. To do so, create a "useless" validation rule on a field that will always be present in your create and update forms :

Text::make('Your field')
    ->rules(function($attribute, $value, $fail) use ($request) {
        $request->merge(['type' => 'holy_meal']);
    })

Please or to participate in this conversation.