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

Dave Wize's avatar

Laravel nova doesn't find the BelongsTo recourse, even though it exists.

I have two nova resources Here is the the account address resource


class AccountAddress extends Resource
{
    /**
     * The model the resource corresponds to.
     *
     * @var string
     */
    public static $model = \App\Models\AccountAddress::class;

    /**
     * The single value that should be used to represent the resource when being displayed.
     *
     * @var string
     */
    public static $title = 'id';

    /**
     * The columns that should be searched.
     *
     * @var array
     */
    public static $search = [
        'id',
    ];

    /**
     * Get the fields displayed by the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),

            BelongsTo::make('Address Type', 'addressType', \App\Nova\AddressTypeMaster::class),
            BelongsTo::make('Location', 'location', \App\Nova\LocationMaster::class),
            BelongsTo::make('Street', 'street', \App\Nova\StreetMaster::class),
            BelongsTo::make('City', 'city', \App\Nova\CityMaster::class),
            BelongsTo::make('State', 'state', \App\Nova\StateMaster::class),
            BelongsTo::make('Zip', 'zip', \App\Nova\ZipcodeMaster::class),

When I try to load this in the browser I'm getting this error

Undefined constant: "App\Nova\StateMaster"

Undefined constant: "App\Nova\LocationMaster"

Here is the State master Resource


class StateMaster extends Resource
{
    /**
     * The model the resource corresponds to.
     *
     * @var string
     */
    public static $model = \App\Models\StateMaster::class;

    /**
     * The single value that should be used to represent the resource when being displayed.
     *
     * @var string
     */
    public static $title = 'state_id';

    /**
     * The columns that should be searched.
     *
     * @var array
     */
    public static $search = [
        'state_id',
    ];

    /**
     * Get the fields displayed by the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function fields(Request $request)
    {
        return [
            ID::make('state_id')->sortable(),

            Number::make('State id')
                ->rules('required', 'integer'),

            Text::make('Country id')
                ->rules('required', 'string'),

            Text::make('State code')
                ->rules('required', 'string'),

            Text::make('State name')
                ->rules('required', 'string'),

            Number::make('Status')
                ->rules('required', 'integer'),


        ];
    }

Here is the relationship on the model

public function location(): BelongsTo
    {
        return $this->belongsTo(LocationMaster::class, 'location', 'location_id');
    }

    public function state(): BelongsTo
    {
        return $this->belongsTo(StateMaster::class, 'state', 'state_id');
    }
0 likes
1 reply
LaryAI's avatar
Level 58

The issue seems to be with the namespace of the related resources. The error message suggests that the StateMaster and LocationMaster resources are not being found.

To fix this, make sure that the namespace of the related resources is correct. In the AccountAddress resource, update the namespace of the related resources to use the correct namespace. For example, if the StateMaster resource is in the App\Nova\Masters namespace, update the BelongsTo field for the state relationship to:

BelongsTo::make('State', 'state', \App\Nova\Masters\StateMaster::class),

Do the same for the other related resources.

Also, make sure that the related resources are registered in the NovaServiceProvider file.

Once the namespaces are corrected and the resources are registered, the error should be resolved.

Please or to participate in this conversation.