Skip to content

fix(laravel): persist morph relations #7170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 2, 2025
Merged

Conversation

Seb33300
Copy link
Contributor

@Seb33300 Seb33300 commented May 23, 2025

Q A
Branch? 4.1
Tickets
License MIT
Doc PR

Morph relations are not working.

Morph relations create 2 columns in the db tables:

For example, if you create a test morph relation:

class Example extends Model
{
    protected $guarded = [];

    public function testable(): MorphTo
    {
        return $this->morphTo();
    }
}
public function up(): void
{
    Schema::create('examples', function (Blueprint $table) {
        $table->id();
        $table->morphs('testable');
    });
}

This will result in 2 columns in the examples table:

  • testable_id (id of the related model)
  • testable_type (fqcn of the related model)

And in the Swagger documentation we can see 2 fields:

  • testable => OK with this PR, it will work as expected if we submit an IRI
  • testable_type => this field is useless and should not be displayed?

@Seb33300 Seb33300 changed the base branch from main to 4.1 May 23, 2025 12:23
@Seb33300 Seb33300 changed the title Morph fix(laravel): persist morph relations May 23, 2025
@soyuka
Copy link
Member

soyuka commented May 24, 2025

testable_type => this field is useless and should not be displayed?

You can hide that field within:

https://github.com/api-platform/core/blob/abb1fac0c6201764589a8f22cb3e1ffd7e02aa16/src/Laravel/Eloquent/Metadata/Factory/Property/EloquentPropertyMetadataFactory.php

Thanks for the patch, it'd be lovely if you could add a test (there are many examples in the workbench directory.

@soyuka soyuka merged commit 84b9679 into api-platform:4.1 Jun 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants