Sabonzy
7 months ago
1264
2
Nova

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

Posted 7 months ago by Sabonzy

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,
            ])
        ];
    }
}

Please sign in or create an account to participate in this conversation.