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

notomato's avatar

Nova MorphMany Relationship

Hi Everybody

I have a polymorphic relationship that an address can belong to a user or an organization. I keep getting the error 'Call to undefined method App\HTTP\Resources\User::singularLabel()'. I tried to use a static method but that didn't seem to help. The code for the files is below.

Thanks

Address.php Model

    public function addressable()
    {
        return $this->morphTo('addressable');
    }
    public function getHashidsConnection()
{
        return 'address';
}

User.php (Model)


    public function organisation()
    {
        return $this->belongsTo('App\Organisation');
    }

    public function addresses()
    {
        return $this->morphMany('App\Address', 'addressable');
    }

}

Address.php (Resource)

    public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),
            Text::make('line_1'),
            Text::make('line_2'),
            Text::make('line_3'),
            Text::make('post_code'),
            Boolean::make('is_primary'),

            MorphTo::make('Addressable')->types([ 
                User::class,
                Organisation::class,
            ])
        ];
    }

    public static function singularLabel()
    {
        return User::class;
    }

User.php (Resource)

    public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),

            Gravatar::make(),

            Text::make('Name')
                ->sortable()
                ->rules('required', 'max:255'),

            Text::make('Email')
                ->sortable()
                ->rules('required', 'email', 'max:254')
                ->creationRules('unique:users,email')
                ->updateRules('unique:users,email,{{resourceId}}'),

            Password::make('Password')
                ->onlyOnForms()
                ->creationRules('required', 'string', 'min:8')
                ->updateRules('nullable', 'string', 'min:8'),

            MorphMany::make('Addresses')
        ];
    }

0 likes
1 reply
keenraven's avatar

Make sure that you are referencing the Nova User and Organisation resources, NOT the Eloquent Models.

So change this:


MorphTo::make('Addressable')->types([
    \App\Models\User::class,
    \App\Models\Organisation::class,
])

To This:


MorphTo::make('Addressable')->types([
    \App\Nova\User::class,
    \App\Nova\Organisation::class,
])

Please or to participate in this conversation.