Laravel CRUD Helper
Read this in other languages: English, 日本語.
CRUD helper for Laravel.
Table of Contents
Details
Install
composer require technote/laravel-crud-helper
Usage
-
Implement
Crudable Contract
andCrudable Trait
.<?php namespace App\Models; use Eloquent; use Illuminate\Database\Eloquent\Model; use Technote\CrudHelper\Models\Contracts\Crudable as CrudableContract; use Technote\CrudHelper\Models\Traits\Crudable; /** * Class Item * @mixin Eloquent */ class Item extends Model implements CrudableContract { use Crudable; /** * @var array */ protected $guarded = [ 'id', ]; }
Routes
CRUD routes are set automatically.
> php artisan route:clear
> php artisan route:list
+--------+-----------+------------------+---------------+-----------------------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+------------------+---------------+-----------------------------------------------------------------+------------+
| | GET|HEAD | api/items | items.index | Technote\CrudHelper\Http\Controllers\Api\CrudController@index | api |
| | POST | api/items | items.store | Technote\CrudHelper\Http\Controllers\Api\CrudController@store | api |
| | GET|HEAD | api/items/{item} | items.show | Technote\CrudHelper\Http\Controllers\Api\CrudController@show | api |
| | PUT|PATCH | api/items/{item} | items.update | Technote\CrudHelper\Http\Controllers\Api\CrudController@update | api |
| | DELETE | api/items/{item} | items.destroy | Technote\CrudHelper\Http\Controllers\Api\CrudController@destroy | api |
+--------+-----------+------------------+---------------+-----------------------------------------------------------------+------------+
Details
Validation
Some validation rules are generated by column settings automatically.
- Type
- integer
- boolean
- numeric
- date
- time
- string
- Length
- Unsigned
- Nullable
- by column name
- url
- phone
Model name
The model name used is determined by api name.
test_items
ex. - to singular:
test_item
- to studly:
TestItem
=> TestItem
Config
Namespace
-
'App\\Models'
- This library does not search recursively.
Prefix
'api'
Middleware
['api']
To Change
-
Run command to generate
config/crud-helper.php
.php artisan vendor:publish --provider="Technote\CrudHelper\Providers\CrudHelperServiceProvider" --tag=config
-
Edit settings.
'namespace' => 'App\\Models\\Crud', 'prefix' => 'api/v1', 'middleware' => [ 'api', 'auth', ],
Search feature
If implement Searchable, you can add search feature.
Laravel Search Helper
api/items?s=keyword