chiariello/laravel-api-crud-maker
最新稳定版本:0.1.0
Composer 安装命令:
composer require chiariello/laravel-api-crud-maker
包简介
Package to create an api CRUD with filters
README 文档
README
Create API CRUD with filters in Laravel project
Installation
Via composer:
composer require chiariello/laravel-api-crud-maker
Add FilterServiceProvider and RequestServiceProvider in config/app.php
'providers' => ServiceProvider::defaultProviders()->merge([ /* * Package Service Providers... */ /* * Application Service Providers... */ App\Providers\AppServiceProvider::class, App\Providers\AuthServiceProvider::class, // App\Providers\BroadcastServiceProvider::class, App\Providers\EventServiceProvider::class, App\Providers\RouteServiceProvider::class, Chiariello\LaravelApiCrudMaker\Providers\FilterServiceProvider::class, // <-- Add This Chiariello\LaravelApiCrudMaker\Providers\RequestServiceProvider::class, // <-- Add This ])->toArray(),
Usage
Create new model with migration and controller:
composer php artisan make:model Flight --migration --controller
Edit the migration as you want
Schema::create('flights', function (Blueprint $table) { $table->id(); $table->string('departure'); $table->string('destination'); $table->timestamps(); });
Add HasFilters trait and fillables attributes in model
namespace App\Models; use Chiariello\LaravelApiCrudMaker\Traits\HasFilters; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Flight extends Model { use HasFactory, HasFilters; protected $fillable = [ 'departure', 'destination' ]; }
Extend CrudController and set $model attribute in Controller class
namespace App\Http\Controllers; use App\Models\Flight; use Chiariello\LaravelApiCrudMaker\Controllers\CrudController; class FlightController extends CrudController { protected string $model = Flight::class; }
Create Filter class under app/Filters the class must have {ModelName}Filter.php name (in this example FlightFilters.php).
Now you need to set the filters array and insert every attribute filter and create a method for every filter.
namespace App\Filters; use Chiariello\LaravelApiCrudMaker\Filters\AbstractFilters; class FlightFilters extends AbstractFilters { protected array $filters = [ 'departure', 'destination' ]; public function departure(string $departure) { $this->like('departure', $departure); } public function destination(string $destination) { $this->like('destination', $destination); } }
create a form request with this name convention {{ModelName}}Request.php
php artisan make:request FlightRequest
Set validation and create and update logic.
namespace App\Http\Requests; use App\Models\Flight; use Illuminate\Foundation\Http\FormRequest; class FlightRequest extends FormRequest { /** * Determine if the user is authorized to make this request. */ public function authorize(): bool { return true; } /** * Get the validation rules that apply to the request. * * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string> */ public function rules(): array { return [ // ]; } public function persist(){ if($this->id){ return Flight ::findOrFail($this->id) ->update($this->all()); } return Flight::create($this->all()); } }
add Route in api.php
use App\Http\Controllers\FlightController; use Chiariello\LaravelApiCrudMaker\Utils\RouteUtility; RouteUtility::controllerRoutes(FlightController::class,'flights');
Credits
统计信息
- 总下载量: 11
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-09-24