laravel-json-api / eloquent

Serialize Eloquent models to JSON API resources
MIT License
12 stars 15 forks source link

[Feature] Add support for soft deleting models #6

Closed lindyhopchris closed 3 years ago

lindyhopchris commented 3 years ago

Updates the package so that it now supports soft deleting. To do this, there is now a SoftDeleteRepository and SoftDeleteHydrator, that extend the existing classes and override the methods required to handle soft delete models.

If a model is soft-deletable, but the schema is not modified, then the model will be soft-deleted on a DELETE request, and will then not appear in the API.

If the soft delete functionality should be exposed to the client, then the LaravelJsonApi\Eloquent\SoftDeletes trait should be added to the schema, and an instance of LaravelJsonApi\Eloquent\Fields\SoftDelete should be added to the schema's fields. Once this is done, a client can soft-delete or restore a resource using a PATCH request, with the standard Eloquent delete/restore events fired as a result. A DELETE request will force delete the resource.

The PR also adds three filters. OnlyTrashed and WithTrashed use the onlyTrashed() and withTrashed() scopes that Eloquent adds for soft deletable models. The WhereTrashed effectively allows resources to be filtered by whether they are trashed or not (which is a slightly different behaviour to the Eloquent withTrashed() scope).

Implements https://github.com/laravel-json-api/laravel/issues/43