berthott / laravel-crudable
Laravel Helper for CRUD Resources
Installs: 1 407
Dependents: 3
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- berthott/laravel-scopeable: ^3.0.0
- berthott/laravel-targetable: ^3.0
- haydenpierce/class-finder: ^0.4.3
- spatie/laravel-query-builder: ^5.0
Requires (Dev)
- doctrine/dbal: ^3.3
- orchestra/testbench: ^8.0
- phpunit/phpunit: ^10.0
README
Laravel-Crudable
A helper for CRUD routes in Laravel.
Easily add a complete CRUD route + controller by adding a trait to your model.
Installation
$ composer require berthott/laravel-crudable
Concept
The package implements a generic approach onto CRUD routes moving the setup of those routes from the several different places (routes / controller) into the model itself. This does'nt prevent you from adding your own custom routes and controllers. There are helper methods if you need the additionally created route in a specific order.
Frontend connection
The index route does not implement any pagination. This is to be considered when implementing the connection to this route as this means a potentially huge payload:
- Try to avoid eagerly loaded relations when they contain a lot of data (do not use them in
$with
array)- To add the information on the relations with minimal data size you can add an attribute holding the relations ids. TODO: This could be done by the package automatically
- If you wan't to eagerly load relations only in the show route you can use
showRelations()
TODO: An optional pagination could help here in the future.
Usage
- Create your table and corresponding model, eg. with
php artisan make:model YourModel -m
- Add the
Crudable
trait to your newly generated model. - The package will register these standard API CRUD routes (see API Resource Routes).
- Index, get
yourmodels/
=> get all entities - Show, get
yourmodels/{yourmodel}
=> get a single entity - Create, post
yourmodels/
=> create a new entity - Update, put
yourmodels/{yourmodel}
=> update an entity - Destroy, delete
yourmodels/{yourmodel}
=> delete an entity
- Index, get
- Additionally it registers
- Destroy many, delete
yourmodels/destroy_many
=> delete many entities by their given ids - Schema, get
yourmodels/schema
=> get the database schema
- Destroy many, delete
- Add relations implementing one of the following methods
attachables()
to attach existing related models to the modelcreatables()
to create new related models and attach them to the modelcustomRelations()
to implement your very own behavior for adding relations to the model
- For more information on how to setup certain features see
\berthott\Crudable\Models\Traits\Crudable
.
Options
To change the default options use
$ php artisan vendor:publish --provider="berthott\Crudable\CrudableServiceProvider" --tag="config"
- Inherited from laravel-targetable
namespace
: String or array with one ore multiple namespaces that should be monitored for the configured trait. Defaults toApp\Models
.namespace_mode
: Defines the search mode for the namespaces.ClassFinder::STANDARD_MODE
will only find the exact matching namespace,ClassFinder::RECURSIVE_MODE
will find all subnamespaces. Defaults toClassFinder::STANDARD_MODE
.prefix
: Defines the route prefix. Defaults toapi
.- General Package Configuration
middleware
: An array of all middlewares to be applied to all of the generated routes. Defaults to['api']
.
Architecture
- The package relies on laravel-targetable to connect specific functionality to Laravel model entities via a trait. (
Crudable
).
Compatibility
Tested with Laravel 10.x.
License
See License File. Copyright © 2023 Jan Bladt.