technote/laravel-crud-helper

CRUD helper for laravel


README

CI Status codecov CodeFactor License: MIT PHP: >=7.4

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

CRUD helper for Laravel.

Packagist

Table of Contents

Details

Install

composer require technote/laravel-crud-helper

Usage

  1. Implement Crudable Contract and 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',
        ];
    }

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
    • email
    • url
    • phone

Model name

The model name used is determined by api name.

ex. test_items

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

=> TestItem

Config

Namespace

  • 'App\\Models'
    • This library does not search recursively.

Prefix

  • 'api'

Middleware

  • ['api']
To Change
  1. Run command to generate config/crud-helper.php.

    php artisan vendor:publish --provider="Technote\CrudHelper\Providers\CrudHelperServiceProvider" --tag=config
    
  2. 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

Author

GitHub (Technote)
Blog