impulse / 脉冲发生器
一个用于从模型创建迁移、控制器和路由的 Laravel 命令行工具
v1.0.2
2020-05-03 15:09 UTC
This package is auto-updated.
Last update: 2024-09-28 15:44:35 UTC
README
一个用于从模型创建迁移、控制器和路由的 Laravel 命令行工具
注意:此包仍在开发阶段,需要大量改进,特别是在迁移生成方面,仅限于创建。请谨慎使用。
安装
$ composer require impulse/pulsifier:v1.0.2
配置
$ php artisan vendor:publish --provider="Impulse\Pulsifier\PulsifierServiceProvider"
这将把 pulsifier.php 添加到您的 Laravel 应用配置目录中。
pulsifier.php
<?php return [ 'model_path' => "Http\\Models\\" ];
如果您正在使用 Laravel 的默认目录结构,请将此文件更改为以下内容:
<?php return [ 'model_path' => "Http\\" ];
然后执行
$ php artisan config:cache
用法
命令模板
$ php artisan pulsify:model { modelName } { --m }
如果您想忽略迁移的生成,不要添加 --m。
示例调用
$ php artisan pulsify:model Product --m
示例集成
要使用此命令,您必须将您的 App 模型扩展为 Impulse\Pulsifier\BaseModel,该模型仍然像常规 Laravel 模型一样工作。
示例模型
产品模型
<?php namespace App; use Impulse\Pulsifier\Helpers\Seek; use Impulse\Pulsifier\Model\BaseModel; class Product extends BaseModel { public function __construct(array $attributes = []) { parent::__construct($attributes); /* Search is optional, if set this will add search function to your controller */ $this->searchable = [ Seek::whereLike('code'), Seek::orWhereLike('name'), Seek::orWhereHas('category', [ Seek::whereLike('name') ]) ]; } /*Regular fillable attribute inhereted from Eloquent*/ protected $fillable = [ 'code', 'name', 'product_category_id', 'unit_id' ]; /*Relationships to be eager load when model is requested from the Generated Controller*/ protected $eager_loaded_relations = [ 'category', 'unit', 'units' ]; /*The command will generate a save() method in the controller and relationships define here will be included in the save method*/ protected $savable_relations = [ 'units' ]; /*Relationships*/ public function units() { return $this->belongsToMany(Unit::class, 'product_units', 'product_id', 'unit_id', 'id'); } public function category() { return $this->belongsTo(ProductCategory::class, 'product_category_id', 'id'); } public function unit() { return $this->belongsTo(Unit::class, 'unit_id', 'id'); } }
单位模型
<?php namespace App; use Illuminate\Database\Eloquent\Model; use Impulse\Pulsifier\Helpers\Seek; use Impulse\Pulsifier\Model\BaseModel; class Unit extends BaseModel { public function __construct(array $attributes = []) { parent::__construct($attributes); $this->searchable = [ Seek::whereLike('code'), Seek::orWhereLike('name'), ]; } protected $fillable = [ 'code', 'name' ]; }
产品分类模型
<?php namespace App; use Impulse\Pulsifier\Helpers\Seek; use Impulse\Pulsifier\Model\BaseModel; class ProductCategory extends BaseModel { public function __construct(array $attributes = []) { parent::__construct($attributes); $this->searchable = [ Seek::whereLike('code'), Seek::orWhereLike('name'), ]; } protected $fillable = [ 'code', 'name' ]; }
示例输出
命令执行后,将为命令中使用的模型生成控制器、路由(可选)和迁移。
控制器
<?php namespace App\Http\Controllers; use Impulse\Pulsifier\Controller\BaseController as PulsifierBaseController; use Illuminate\Http\Request; use App\Product; use App\Unit; class ProductController extends PulsifierBaseController { public function __construct(Request $request) { parent::__construct($request); $this->relationShips = ['category', 'unit', 'units']; } public function index() { $query = Product::with($this->relationShips) ->when(!empty($this->searchTerm), function ($query) { $query->where('code', 'like', '%' . $this->searchTerm . '%') ->orWhere('name', 'like', '%' . $this->searchTerm . '%') ->orWhereHas('category', function ($category) { $category->where('name', 'like', '%' . $this->searchTerm . '%'); }); }); $products = ($this->perPage != 0) ? $query->paginate($this->perPage) : $query->get(); return response($products); } public function save() { $data = $this->request->all(); $id = isset($data['id']) ? $data['id'] : -1; $product = Product::updateOrCreate( ['id' => $id], $data ); if (isset($data['units']) && count($data['units']) != 0) { $unit = $product->units->pluck('pivot'); $pivoted_ids = $unit->pluck('unit_id'); $pivoted_ids->concat(collect($data['units'])->pluck('unit_id')); $product->units()->sync($pivoted_ids->unique()->all()); } if (empty($product)) return response("An error occur during save", 500); return $this->get($product->id); } public function get($id) { $product = Product::with($this->relationShips)->find($id); if (empty($product)) return response("Record not found", 404); return response($product); } public function destroy($id) { $product = Product::find($id); if (empty($product)) return response("Record not found", 404); if (!$product->delete()) return response("An error occur during delete", 500); return response("Record deleted"); } }
路由
<?php use Illuminate\Http\Request; use Illuminate\Support\Facades\Route; Route::middleware('auth:api')->get('/user', function (Request $request) { return $request->user(); }); Route::prefix('products')->name('products')->group(function(){ Route::get('/','ProductController@index')->name('index'); Route::get('/get/{id}','ProductController@get')->name('get')->where('id', '[0-9]+'); Route::delete('/destroy/{id}','ProductController@destroy')->name('destroy')->where('id', '[0-9]+'); Route::post('/save','ProductController@save')->name('save'); });