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

Sabonzy's avatar

Laravel Nova one-to-many polymorphic relationship singleLabel/uriKey exception

When defining one to many polymorphic relations on Laravel Nova resource it throws an exception Call to undefined method singularLabel() or Call to undefined method uriKey() on model.

If i define the two static methods on the models what should the the returned type be.

Below is the related models and the resource.

Eloquent Models:

class QuestionDiagram extends Model
{
     public function diagramable()
     {
       	 return $this->morphTo();
     }
   
}

class Preamble extends Model
{

     public function diagrams()
     {
        return $this->morphMany(QuestionDiagram::class, 'diagramable');
     }

     public static function singularLabel()
     {
	// what should be returned
     }

     public static function uriKey()
     {
	// what should be returned
     }
  	
}
class Question extends Model
{

  public function diagrams()
    {
        return $this->morphMany(QuestionDiagram::class, 'diagramable');
    }

 public static function singularLabel()
   	 {
	// what should be returned
   	 }

    public static function uriKey()
    	{
	// what should be returned
   	 }
  	
}

Nova Resource:

class QuestionDiagram extends Resource
{
 public function fields(Request $request)
  	  {
        return [
            ID::make()->sortable(),
            Image::make('Diagram', 'path'),
            MorphTo::make('diagramable')->types([
                Preamble::class,
                Question::class,
            ])
        ];
    }
}

0 likes
2 replies
Sabonzy's avatar

My bad I was using models as the types instead of nova resource.

Thanks.

Please or to participate in this conversation.