Skip to content

v3.1.0

Choose a tag to compare

@GautierDele GautierDele released this 10 Nov 13:31
7f5d46e

A minor release of 3.x: relations choose the mutation operations they accept. Upgrade with composer update lomkit/laravel-rest-api, then read the 3.1 section of the upgrade guide.

Relation operations

  • ->withoutOperations(MutationOperation ...$operations) withdraws operations from a relation, ->onlyOperations(MutationOperation ...$operations) keeps only some, and ->onlyOperations() makes the relation read-only:

    BelongsTo::make('team', TeamResource::class)->withoutOperations(MutationOperation::Detach),
    BelongsToMany::make('tags', TagResource::class)->onlyOperations(MutationOperation::Attach, MutationOperation::Detach),
  • Validation, /details and the OpenAPI document follow: a withdrawn operation answers 422.

  • A many-to-many relation that does not accept detach does not accept toggle either, and its sync needs "without_detaching": true.

  • A relation that does not accept update does not write its related rows through sync, toggle or nested relations (pivot values stay writable).

Required links

  • A detach that would set a foreign key or morph type that does not accept null answers a 422 on the key of the entry, instead of a database error.
  • rest:resource --generate and rest:quick-start add ->withoutOperations(MutationOperation::Detach) to a relation whose foreign key or morph type does not accept null.
  • rest:check reports each relation that accepts detach while its link column does not accept null. It reads the database schema: a missing table is skipped, an unreadable schema is reported.

Check after upgrading

  • rest:check is stricter: a resource generated with 3.0 on a NOT NULL foreign key now fails it until the relation withdraws detach or the column becomes nullable.
  • On MySQL without strict mode, a detach on a NOT NULL foreign key used to write 0 or ''; it now answers 422.

Documentation: https://laravel-rest-api.lomkit.com