最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

eloquent - Laravel - api-platform: Full Api-platform usage example with larvel model - Stack Overflow

programmeradmin0浏览0评论

Can anyone provide full example usage of larvael model with api-platform. With pagination, filters, groups, etc.

I use api-platform with symfony but I want to use apip with laravel as well but facing some difficulties with the syntax.

And Not sure if this is an issue or i am missing any configs, in symfony apip returns response with some additional fields like this

"@context": "/contexts/PushToken", "@id": "/push-tokens/1efe5671-959d-6128-8045-21b158a1f424", "@type": "PushToken", "fcm": "dXNxMksxa09ZVmJOVHBLZ3JYZzoyQUJDREVGR0hJSktMTU5PUFFSVFVWV1hZWg=="

but in laravel its just returning

"fcm": "dXNxMksxa09ZVmJOVHBLZ3JYZzoyQUJDREVGR0hJSktMTU5PUFFSVFVWV1hZWg=="

This is what my current code look like:

<?php

declare(strict_types=1);

namespace App\Models;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Post;
use App\Api\Processor\PushToken\PushTokenRegisterProcessor;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Symfony\Component\Serializer\Attribute\Groups;

#[ApiResource(
    normalizationContext: [
        'groups' => [
            'PushToken:V',
        ],
    ],
    denormalizationContext: [
        'groups' => [
            'PushToken:W',
        ],
    ],
    operations: [
        new Post(
            rules : [
                'fcm' => 'required_without:apns|prohibits:apns',
                'apns' => 'required_without:fcm|prohibits:fcm',
            ],
            uriTemplate: '/push-tokens',
            processor: PushTokenRegisterProcessor::class,
        ),
    ]
)]
#[ApiProperty(
    property: 'fcm',
    serialize: new Groups([
        'PushToken:W',
    ])
)]
#[ApiProperty(
    property: 'apns',
    serialize: new Groups([
        'PushToken:W',
    ])
)]
class PushToken extends Model
{
    use HasUlids;

    public $timestamps = false;

    protected $fillable = [
        'user_id',
        'fcm',
        'apns',
        'last_registered_at',
        'delivery_failed_at',
    ];

    /**
     * @return BelongsTo<User, $this>
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }
}

Processor

final class PushTokenRegisterProcessor implements ProcessorInterface
{
    /**@param PushToken $data */
    public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): PushToken
    {
        Assert::isInstanceOf($data, PushToken::class);
        Assert::isInstanceOf($user = Auth::user(), User::class);

        $attributes = null !== $data->fcm ? ['fcm' => $data->fcm] : ['apns' => $data->apns];

        return PushToken::updateOrCreate($attributes, [
            'user_id' => $user->id,
            'last_registered_at' => \now(),
        ]);
    }
}
发布评论

评论列表(0)

  1. 暂无评论