|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <dunglas@gmail.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace Workbench\App\Models; |
| 15 | + |
| 16 | +use ApiPlatform\Metadata\ApiProperty; |
| 17 | +use ApiPlatform\Metadata\ApiResource; |
| 18 | +use ApiPlatform\Metadata\Post; |
| 19 | +use Illuminate\Database\Eloquent\Model; |
| 20 | +use Illuminate\Database\Eloquent\Relations\MorphMany; |
| 21 | +use Symfony\Component\Serializer\Attribute\Groups; |
| 22 | + |
| 23 | +#[ApiResource(operations: [ |
| 24 | + new Post( |
| 25 | + denormalizationContext: ['groups' => ['comments']], |
| 26 | + normalizationContext: ['groups' => ['comments']], |
| 27 | + ), |
| 28 | +])] |
| 29 | +#[ApiProperty(serialize: new Groups(['comments']), property: 'title')] |
| 30 | +#[ApiProperty(serialize: new Groups(['comments']), property: 'content')] |
| 31 | +#[ApiProperty(serialize: new Groups(['comments']), property: 'comments')] |
| 32 | +class PostWithMorphMany extends Model |
| 33 | +{ |
| 34 | + protected $table = 'posts_with_morph_many'; |
| 35 | + protected $fillable = ['title', 'content']; |
| 36 | + |
| 37 | + public function comments(): MorphMany |
| 38 | + { |
| 39 | + return $this->morphMany(CommentMorph::class, 'commentable'); |
| 40 | + } |
| 41 | +} |
0 commit comments