Skip to content

[Laravel] How to use Serialization Groups on Model relations #7153

Closed
@shahzaibtariqbutt

Description

@shahzaibtariqbutt

When using Serialization Groups on Model relations throws RuntimeException
"message": "This database driver does not support retrieving user-defined types.",
"exception": "RuntimeException",

In this example number is being exposed but as soon as I try to expose user it throws this exception.

#[ApiResource(
    operations: [
        new GetCollection(
            uriTemplate: '/orders,
            paginationEnabled: true,
            paginationItemsPerPage: 10,
            normalizationContext: [
                'groups' => [
                    'List'
                ]
            ]
        ),
    ]
)]
.
.
.
#[ApiProperty(property: 'number', serialize: new Groups(['List']))]
#[ApiProperty(property: 'user', serialize: new Groups(['List']))]

class Order extends Model
{

protected $fillable = [
        'number',
    ];

    /**
     * Get the user that owns the order.
     *
     * @return BelongsTo<User, $this>
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }
}

even if I do something like this, it still does not expose as it does in symfony.

#[Groups(['List'])]
   public function getUserfullName(): string
   {
       Assert::notNull($user = $this->user()->first());

       return $user->full_name;
   }

As a developer experienced with API Platform in Symfony, I've found it challenging to adapt to its usage within Laravel due to limited examples and documentation. I kindly request the addition of comprehensive guides or tutorials that cover:
Implementing serialization groups in Laravel
Managing relationships and nested resources effectively

I believe such resources would greatly assist developers in integrating API Platform into Laravel projects and encourage wider adoption within the Laravel community.

Thank you for your consideration and for the excellent work on API Platform!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions