laravel-crud-helper

CRUD helper for Laravel

MIT License

Downloads
2.4K
Stars
1
Committers
4

Laravel CRUD Helper

Read this in other languages: English, 日本語.

Laravel用CRUDヘルパー

Packagist

Table of Contents

インストール

composer require technote/laravel-crud-helper

使用方法

  1. Crudable ContractCrudable 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 は自動で設定されます。

> 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        |
+--------+-----------+------------------+---------------+-----------------------------------------------------------------+------------+

詳細

バリデーション

いくつかのバリデーションルールはカラム設定から自動で生成されます。

  • Type
    • integer
    • boolean
    • numeric
    • date
    • time
    • string
  • Length
  • Unsigned
  • Nullable
  • by column name
    • email
    • url
    • phone

モデル名

使用されるモデル名はAPI名によって決まります。

例:test_items

  1. to singular: test_item
  2. to studly: TestItem

=> TestItem

Config

Namespace

  • 'App\\Models'
    • このライブラリは再帰的に検索しません。

Prefix

  • 'api'

Middleware

  • ['api']
変更方法
  1. config/crud-helper.php を生成するためのコマンドを実行

    php artisan vendor:publish --provider="Technote\CrudHelper\Providers\CrudHelperServiceProvider" --tag=config
    
  2. 設定を変更

    'namespace'  => 'App\\Models\\Crud',
    'prefix'     => 'api/v1',
    'middleware' => [
        'api',
        'auth',
    ],
    

検索機能

Searchable が実装されている場合、検索機能が追加されます。

Laravel Search Helper

api/items?s=keyword

Author

GitHub (Technote) Blog

Package Rankings
Top 19.15% on Packagist.org
Badges
Extracted from project README
CI Status codecov CodeFactor License: MIT PHP: >=7.4