Skip to content

fix(laravel): Allow LinksHandler to handle polymorphic relationships #7231

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 2 commits into from
Jun 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 36 additions & 20 deletions src/Laravel/Eloquent/State/LinksHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace ApiPlatform\Laravel\Eloquent\State;

use ApiPlatform\Metadata\Exception\OperationNotFoundException;
use ApiPlatform\Metadata\Exception\RuntimeException;
use ApiPlatform\Metadata\GraphQl\Operation;
use ApiPlatform\Metadata\GraphQl\Query;
use ApiPlatform\Metadata\HttpOperation;
Expand All @@ -22,6 +23,11 @@
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasOneOrMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\Relation;

/**
* @implements LinksHandlerInterface<Model>
Expand Down Expand Up @@ -101,34 +107,44 @@ private function buildQuery(Builder $builder, Link $link, mixed $identifier): Bu
}

if ($from = $link->getFromProperty()) {
$relation = $this->application->make($link->getFromClass());
$relationQuery = $relation->{$from}();
if (!method_exists($relationQuery, 'getQualifiedForeignKeyName') && method_exists($relationQuery, 'getQualifiedForeignPivotKeyName')) {
/** @var Model $relatedInstance */
$relatedInstance = $this->application->make($link->getFromClass());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line makes me think about #7166, its another subject but do you have any idea on how we could do otherwise to instantiate this class?

Copy link
Contributor Author

@jonerickson jonerickson Jun 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what needs to happen in the item/collection provider or property accessor is something like loadMissing on the relations that are discovered through the eloquent property metadata service. This error happens when calling a relation that hasn't already been hydrated in the class. By calling loadMissing we can hopefully prevent that. I'll play around with it and see what I can come up with.

Copy link
Contributor Author

@jonerickson jonerickson Jun 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, we don't need to do anything anymore. This switched fix #7166 as well. See #7166 (comment)

$relatedInstance->setAttribute($relatedInstance->getKeyName(), $identifier);
$relatedInstance->exists = true;

/** @var Relation<Model, Model, mixed> $relation */
$relation = $relatedInstance->{$from}();

if ($relation instanceof MorphTo) {
throw new RuntimeException('Cannot query directly from a MorphTo relationship.');
}

if ($relation instanceof BelongsTo) {
return $builder->getModel()
->join(
$relationQuery->getTable(), // @phpstan-ignore-line
$relationQuery->getQualifiedRelatedPivotKeyName(), // @phpstan-ignore-line
$builder->getModel()->getQualifiedKeyName()
)
->where(
$relationQuery->getQualifiedForeignPivotKeyName(), // @phpstan-ignore-line
$relation->getParent()->getTable(),
$relation->getParent()->getQualifiedKeyName(),
$identifier
)
->select($builder->getModel()->getTable().'.*');
);
}

if (method_exists($relationQuery, 'dissociate')) {
return $builder->getModel()
->join(
$relationQuery->getParent()->getTable(), // @phpstan-ignore-line
$relationQuery->getParent()->getQualifiedKeyName(), // @phpstan-ignore-line
$identifier
);
if ($relation instanceof HasOneOrMany || $relation instanceof BelongsToMany) {
return $relation->getQuery();
}

if (method_exists($relation, 'getQualifiedForeignKeyName')) {
return $relation->getQuery()->where(
$relation->getQualifiedForeignKeyName(),
$identifier
);
}

return $builder->getModel()->where($relationQuery->getQualifiedForeignKeyName(), $identifier);
throw new RuntimeException(\sprintf('Unhandled or unknown relationship type: %s for property %s on %s', $relation::class, $from, $relatedInstance::class));
}

return $builder->where($builder->getModel()->qualifyColumn($link->getIdentifiers()[0]), $identifier);
return $builder->where(
$builder->getModel()->qualifyColumn($link->getIdentifiers()[0]),
$identifier
);
}
}
51 changes: 51 additions & 0 deletions src/Laravel/Tests/EloquentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
use Illuminate\Support\Str;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase;
use Workbench\App\Models\PostWithMorphMany;
use Workbench\Database\Factories\AuthorFactory;
use Workbench\Database\Factories\BookFactory;
use Workbench\Database\Factories\CommentMorphFactory;
use Workbench\Database\Factories\GrandSonFactory;
use Workbench\Database\Factories\PostWithMorphManyFactory;
use Workbench\Database\Factories\WithAccessorFactory;

class EloquentTest extends TestCase
Expand Down Expand Up @@ -444,6 +447,17 @@ public function testBelongsTo(): void
$this->assertEquals($json['sons'][0], '/api/grand_sons/1');
}

public function testHasMany(): void
{
GrandSonFactory::new()->count(1)->create();

$res = $this->get('/api/grand_fathers/1/grand_sons', ['Accept' => ['application/ld+json']]);
$json = $res->json();
$this->assertEquals($json['@id'], '/api/grand_fathers/1/grand_sons');
$this->assertEquals($json['totalItems'], 1);
$this->assertEquals($json['member'][0]['@id'], '/api/grand_sons/1');
}

public function testRelationIsHandledOnCreateWithNestedData(): void
{
$cartData = [
Expand Down Expand Up @@ -538,4 +552,41 @@ public function testPostWithEmptyMorphMany(): void
'comments' => [['content' => 'hello']],
]);
}

public function testPostCommentsCollectionFromMorphMany(): void
{
PostWithMorphManyFactory::new()->create();

CommentMorphFactory::new()->count(5)->create([
'commentable_id' => 1,
'commentable_type' => PostWithMorphMany::class,
]);

$response = $this->getJson('/api/post_with_morph_manies/1/comments', [
'accept' => 'application/ld+json',
]);
$response->assertStatus(200);
$response->assertJsonCount(5, 'member');
}

public function testPostCommentItemFromMorphMany(): void
{
PostWithMorphManyFactory::new()->create();

CommentMorphFactory::new()->count(5)->create([
'commentable_id' => 1,
'commentable_type' => PostWithMorphMany::class,
])->first();

$response = $this->getJson('/api/post_with_morph_manies/1/comments/1', [
'accept' => 'application/ld+json',
]);
$response->assertStatus(200);
$response->assertJson([
'@context' => '/api/contexts/CommentMorph',
'@id' => '/api/post_with_morph_manies/1/comments/1',
'@type' => 'CommentMorph',
'id' => 1,
]);
}
}
32 changes: 30 additions & 2 deletions src/Laravel/workbench/app/Models/CommentMorph.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,40 @@
namespace Workbench\App\Models;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\NotExposed;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Link;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Symfony\Component\Serializer\Attribute\Groups;

#[NotExposed]
#[ApiResource(
operations: [
new GetCollection(
uriTemplate: '/post_with_morph_manies/{id}/comments',
uriVariables: [
'id' => new Link(
fromProperty: 'comments',
fromClass: PostWithMorphMany::class,
),
]
),
new Get(
uriTemplate: '/post_with_morph_manies/{postId}/comments/{id}',
uriVariables: [
'postId' => new Link(
fromProperty: 'comments',
fromClass: PostWithMorphMany::class,
),
'id' => new Link(
fromClass: CommentMorph::class,
),
]
),
]
)]
#[ApiProperty(identifier: true, serialize: new Groups(['comments']), property: 'id')]
#[ApiProperty(serialize: new Groups(['comments']), property: 'content')]
class CommentMorph extends Model
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Workbench\App\Models\Comment;

/**
* @template TModel of \Workbench\App\Models\Author
* @template TModel of \Workbench\App\Models\Comment
*
* @extends \Illuminate\Database\Eloquent\Factories\Factory<TModel>
*/
Expand Down
46 changes: 46 additions & 0 deletions src/Laravel/workbench/database/factories/CommentMorphFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Workbench\Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Workbench\App\Models\CommentMorph;

/**
* @template TModel of \Workbench\App\Models\CommentMorph
*
* @extends \Illuminate\Database\Eloquent\Factories\Factory<TModel>
*/
class CommentMorphFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var class-string<TModel>
*/
protected $model = CommentMorph::class;

/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'commentable_id' => PostWithMorphManyFactory::new(),
'commentable_type' => PostWithMorphManyFactory::class,
'content' => fake()->text(),
];
}
}
2 changes: 1 addition & 1 deletion src/Laravel/workbench/database/factories/PostFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Workbench\App\Models\Post;

/**
* @template TModel of \Workbench\App\Models\Author
* @template TModel of \Workbench\App\Models\Post
*
* @extends \Illuminate\Database\Eloquent\Factories\Factory<TModel>
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Workbench\Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Workbench\App\Models\PostWithMorphMany;

/**
* @template TModel of \Workbench\App\Models\PostWithMorphMany
*
* @extends \Illuminate\Database\Eloquent\Factories\Factory<TModel>
*/
class PostWithMorphManyFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var class-string<TModel>
*/
protected $model = PostWithMorphMany::class;

/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'title' => fake()->unique()->sentence(10),
'content' => fake()->sentences(10, true),
];
}
}
Loading