geekyants/laravel-usedb is a Laravel package for generic crud operation.
It currently has 0 GitHub stars and 25 downloads on Packagist.
Install it with composer require geekyants/laravel-usedb.
Discover more Laravel packages by geekyants
or browse all Laravel packages to compare alternatives.
Last updated
Laravel-usedb is a Generic-CRUD composer package for Laravel apps which eliminates the need to write Apis for each and every model in a Laravel app.
The idea came out to simplify the job of developers who had to create a similar type of CRUD apis for different projects. Using this package, you can perform CRUD operations to any model in your project.
#COMPOSER
composer require geekyants/laravel-usedb
After installation, run the command below to move package's config files to your project config.
php artisan vendor:publish --tag=config
All API request is received at <APP_URL>/usedb and method type will always of type POST.
The fields which are mandatory, while sending requests are as follows:
collection: It contains the name of the model on which operation has to be performed.
operation: Name of the operation.
payload: It contains different values on the basis of the type of operation.
Mandatory fields in the payload
| Operation | Fields | | --------- | ----------- | | create | data | | findOne | where | | findMany | skip, take | | update | data, where | | delete | where |
The middleware property of the usedb.php config file contains entries for the middleware to be included with our route. If you would like to assign middleware to routes, make an entry in the middleware. By default, usedb, and model-usedb middlewares are added in the config.
The modelPath property of usedb.php contains the path of the directory from which the Model class has to be loaded. By default, its value is assigned to "App\Models\" which is Laravel's default directory for models.
Authorization can be done in two ways: Gates and Policies. After creating them, you need to mention them in the usedb config file.
'gates' => [
'modelName' => [
'update' => [],
'delete' => [],
'create' => [],
'findOne' => [],
'findMany' => []
]
]
modelName represents the name of the model in which you want to apply gates. update, delete, create, findOne, and findMany maps to the array of gates to be applied to their respective operations.
policies property of type array in the permissions field.
The syntax for the same:'policies' => [
'update' => '',
'delete' => '',
'create' => '',
'findOne' => '',
'findMany' => ''
]
]
modelName represents the name of the model in which you want to apply policies. update, delete, create, findOne, and findMany maps to the policy to be applied to their respective operations.
When you want objects to be returned with their associated models, then you have to mention the property to access child elements from a parent in the include property of payload field of the JSON provided.
For example, we have an OneToMany relation between Blogs and comments. Now we want to retrieve the blog with its associated comments, then its JSON will be:
{
"collection":"Blog",
"operation":"findOne",
"payload": {
"where":{
"id": 139
},
"include":{
"comments":{}
}
}
}
Let's say, we have a Post model having a single property: caption. CRUD example for Post model are as follows:
{
"collection":"Post",
"operation":"create",
"payload": {
"data":
{
"caption": "Caption of the post"
}
}
}
{
"collection":"Post",
"operation":"findOne",
"payload": {
"where":
{
"id": 1
}
}
}
{
"collection":"Post",
"operation":"update",
"payload": {
"data": {
"caption": "changed caption"
},
"where": {
"id" : 10
}
}
}
{
"collection":"Post",
"operation":"delete",
"payload": {
"where": {
"id" : 3
}
}
}
{
"collection":"Post",
"operation":"findMany",
"payload": {
"where": {
"caption": "hello"
},
"skip": 1,
"take": 5
}
}