laravel / nova-issues

556 stars 34 forks source link

Getting "Laravel\Nova\Fields\BelongsTo::buildMorphableQuery does not exist" on a Polymorphic resource #6473

Closed lukejwatts closed 4 months ago

lukejwatts commented 4 months ago

Description:

I have a polymorhic table which has a belongs to relationship on it also. When I attempt to select the resource (project_phaseable_type) from the dropdown I get the following error:

image

I'm assuming it's because of the BelongsTo field in my fields array? Or I've set something up wrong (Please see the code snippets below for migrations, model and resource definitions)

Am I using the wrong morphTo/Many etc? The models relationships all work as expected for my use case which is many-to-many from the example, but it's actually a one-to-one relationship in practice.

Polymorphic Migration Schema

create_project_phases_table migration:

Schema::create('project_phases', function (Blueprint $table) {
    $table->id();
    $table->unsignedBigInteger('phase_id');
    $table->morphs('project_phaseable');
    $table->timestamps();

    // Ensures we can only have one phase per "project_phaseable_type"
    $table->unique(['phase_id', 'project_phaseable_type', 'project_phaseable_id'], 'project_phaseable_polymorphic_unique');

    $table->foreign('phase_id')->references('id')->on('phases');
});

ProjectPhase model

class ProjectPhase extends Model
{
    use HasFactory;

    /**
     * Get the parent project_phaseable model (family, estate, property).
     */
    public function projectPhaseable()
    {
        return $this->morphTo();
    }

    public function phase(): BelongsTo
    {
        return $this->belongsTo(Phase::class, 'phase_id', 'id');
    }
}

ProjectPhase resource (fields)

public function fields(NovaRequest $request)
    {
        return [
            ID::make()->sortable(),
            BelongsTo::make('Phase', 'projectPhaseable', Phase::class),
            MorphTo::make('Project Phaseable', 'projectPhaseable')->types([
                Estate::class,
                Family::class,
                Property::class,
            ])->default(Estate::class),
        ];
    }

Detailed steps to reproduce the issue on a fresh Nova installation:

  1. Setup a polymorphic relationship as per the example here in the docs: https://laravel.com/docs/10.x/eloquent-relationships#many-to-many-polymorphic-relations
  2. Add BelongsTo field in you Taggable resource

Thanks in advance!

Luke

crynobone commented 4 months ago

BelongsTo::make('Phase', 'projectPhaseable', Phase::class),

Bad relationship name here.

lukejwatts commented 4 months ago

BelongsTo::make('Phase', 'projectPhaseable', Phase::class),

Bad relationship name here.

So should it be BelongsTo::make('Phase', 'projectPhase', Phase::class) or something else?

crynobone commented 4 months ago

SmartSelect_20240723_093238_Chrome

From the documentation , and you can also refer to https://laravel.com/docs/11.x/eloquent-relationships#one-to-one-defining-the-inverse-of-the-relationship