WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm working with API Platform for Laravel and encountering difficulties managing BelongsToMany relationships. While BelongsTo and HasMany relationships work seamlessly by sending IRIs in the request body, BelongsToMany doesn't seem to follow the same pattern.
I'm trying to add users (participants) to a trip when creating (POST) or updating (PATCH) a trip.
Based on my understanding of the documentation, subresources in API Platform are designed for GET operations so that is not what I need here.
I attempted to use a DTO with a StateProcessor to send new participants and their permissions (view/edit) for a Trip. However, I encountered a 403 error because the ResourceAccessChecker evaluates the Gate with the DTO instance instead of the actual Trip model, causing the authorization to fail.
This behavior made me realize what @soyuka explained here.
I also looked into the default 'PersistProcessor' for Laravel and found that BelongsToMany relationships are not handled.
At this point, it feels like I’m fighting against the framework’s design. I’m considering creating a dedicated controller with a custom route to handle this.
Am I missing something, or is this the correct approach in API Platform with Laravel?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I'm working with API Platform for Laravel and encountering difficulties managing BelongsToMany relationships. While BelongsTo and HasMany relationships work seamlessly by sending IRIs in the request body, BelongsToMany doesn't seem to follow the same pattern.
I'm trying to add users (participants) to a trip when creating (POST) or updating (PATCH) a trip.
Here's the Trip Model:
#[ApiResource( operations: [ new Get(), new GetCollection(), new Post( rules: StoreTripRequest::class ), new Patch( rules: UpdateTripRequest::class ), new Delete() ] )] class Trip extends Model { use HasFactory, HasUuids; /** * The attributes that are mass assignable. * * @var list<string> */ protected $fillable = [ 'label', 'description', 'distance', 'duration', 'is_public', 'geojson', ]; protected $casts = [ 'is_public' => 'boolean', ]; public function author(): BelongsTo { return $this->belongsTo(User::class); } public function stops(): HasMany { return $this->hasMany(Stop::class); } public function participants(): BelongsToMany { return $this->belongsToMany(User::class, 'user_trip', 'trip_id', 'user_id'); } public function luggages(): HasMany { return $this->hasMany(Luggage::class); }For BelongsTo (author) and HasMany (stops) relationships, sending IRIs in the request body works as expected during a POST or PATCH operation:
{ "label": "My Trip", "description": "", "distance": null, "duration": null, "is_public": true, "geojson": null, "author": "/api/users/01977e42-9412-713c-861c-93cfbdd9c077", "stops": [ "/api/stops/01977e42-98cd-73fb-9fae-e76b52fed90f", "/api/stops/01977e42-98c1-73c7-b35e-d3790fba6ba0" ], }However, when I try to do something similar for the participants (BelongsToMany) relation by including a list of user IRIs:
I receive the following error:
It's not correctly interacting with the BelongsToMany relation to manage entries in the user_trip pivot table.
Thank you in advance for your help!
Beta Was this translation helpful? Give feedback.
All reactions