Read this in other languages: English, 日本語.
Laravel用CRUDヘルパー
composer require technote/laravel-crud-helper
Crudable Contract
と Crudable 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',
];
}
CRUD routes は自動で設定されます。
> 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 |
+--------+-----------+------------------+---------------+-----------------------------------------------------------------+------------+
いくつかのバリデーションルールはカラム設定から自動で生成されます。
使用されるモデル名はAPI名によって決まります。
test_items
test_item
TestItem
=> TestItem
'App\\Models'
'api'
['api']
config/crud-helper.php
を生成するためのコマンドを実行
php artisan vendor:publish --provider="Technote\CrudHelper\Providers\CrudHelperServiceProvider" --tag=config
設定を変更
'namespace' => 'App\\Models\\Crud',
'prefix' => 'api/v1',
'middleware' => [
'api',
'auth',
],
Searchable
が実装されている場合、検索機能が追加されます。
api/items?s=keyword