Skip to content
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
3 changes: 2 additions & 1 deletion src/Laravel/Eloquent/State/PersistProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use ApiPlatform\Metadata\HttpOperation;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
Expand Down Expand Up @@ -62,7 +63,7 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
if (HasMany::class === $relation['type'] || MorphMany::class === $relation['type']) {
$rel = $data->{$relation['name']};

if (!\is_array($rel)) {
if (!\is_array($rel) && !$rel instanceof Collection) {
throw new RuntimeException('To-Many relationship is not a collection.');
}

Expand Down
15 changes: 15 additions & 0 deletions src/Laravel/Tests/EloquentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -523,4 +523,19 @@ public function testRelationIsHandledOnCreateWithNestedDataToMany(): void
],
]);
}

public function testPostWithEmptyMorphMany(): void
{
$response = $this->postJson('/api/post_with_morph_manies', [
'title' => 'My first post',
'content' => 'This is the content of my first post.',
'comments' => [['content' => 'hello']],
], ['accept' => 'application/ld+json', 'content-type' => 'application/ld+json']);
$response->assertStatus(201);
$response->assertJson([
'title' => 'My first post',
'content' => 'This is the content of my first post.',
'comments' => [['content' => 'hello']],
]);
}
}
33 changes: 33 additions & 0 deletions src/Laravel/workbench/app/Models/CommentMorph.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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\App\Models;

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

#[NotExposed]
#[ApiProperty(serialize: new Groups(['comments']), property: 'content')]
class CommentMorph extends Model
{
protected $table = 'comments_morph';
protected $fillable = ['content'];

public function commentable(): MorphTo
{
return $this->morphTo();
}
}
41 changes: 41 additions & 0 deletions src/Laravel/workbench/app/Models/PostWithMorphMany.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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\App\Models;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Post;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Symfony\Component\Serializer\Attribute\Groups;

#[ApiResource(operations: [
new Post(
denormalizationContext: ['groups' => ['comments']],
normalizationContext: ['groups' => ['comments']],
),
])]
#[ApiProperty(serialize: new Groups(['comments']), property: 'title')]
#[ApiProperty(serialize: new Groups(['comments']), property: 'content')]
#[ApiProperty(serialize: new Groups(['comments']), property: 'comments')]
class PostWithMorphMany extends Model
{
protected $table = 'posts_with_morph_many';
protected $fillable = ['title', 'content'];

public function comments(): MorphMany
{
return $this->morphMany(CommentMorph::class, 'commentable');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
public function up(): void
{
Schema::create('posts_with_morph_many', function (Blueprint $table): void {
$table->id();
$table->string('title');
$table->text('content');
$table->timestamps();
});

Schema::create('comments_morph', function (Blueprint $table): void {
$table->id();
$table->text('content');
$table->morphs('commentable');
$table->timestamps();
});
}

public function down(): void
{
Schema::dropIfExists('posts_with_morph_many');
Schema::dropIfExists('comments_morph');
}
};
Loading