ahmed-aliraqi / crud-generator
此包是用于为 laravel-modules/scaffolding 生成简单 CRUD 的实用工具
v4.1.0
2023-03-03 21:29 UTC
Requires
- php: >=8.0
- illuminate/console: >=9.0
- illuminate/support: >=9.0
README
简介
此包是用于为 laravel-modules/scaffolding 生成简单 CRUD 的实用工具
将要生成的文件包括
- 语言文件(ar & en)
- 面包屑文件
- 视图文件
- API 资源文件
- 迁移文件
- 工厂文件
- 策略文件
- 控制器文件
- 模型文件
- 请求文件
- 过滤器文件
- 测试文件
安装
composer require ahmed-aliraqi/crud-generator --dev
配置
您应使用以下命令添加配置文件以配置支持的资源。
php artisan vendor:publish --provider="AhmedAliraqi\CrudGenerator\CrudServiceProvider"
然后在 routes/dashboard.php 和 routes/api.php 文件中添加以下注释行
/* The routes of generated crud will set here: Don't remove this line */
并在 resources/views/layouts/sidebar.blade.php 文件中添加以下注释行
{{-- The sidebar of generated crud will set here: Don't remove this line --}}
用法
例如,如果您想生成一个名为 category 的新 CRUD,请确保它的阿拉伯语单词已定义在配置文件中的 arabicWords 中,然后使用以下 artisan 命令
php artisan make:crud category
如果 CRUD 可翻译,请使用 translatable 选项
php artisan make:crud category --translatable
如果 CRUD 有媒体,请使用 has-media 选项
php artisan make:crud category --has-media
您还可以同时使用这两个选项来生成可翻译且带有媒体的 CRUD。
php artisan make:crud category --translatable --has-media
账户类型克隆器
php artisan account:clone customer merchant
此命令将
customer账户类型克隆到名为merchant的另一个类型中
一些文件需要手动修改,例如
- 在
app/Models/User.php中添加新类型的常量
/** * The code of merchant type. * * @var string */ const MERCHANT_TYPE = 'merchant';
然后在 childTypes 数组中注册该类型
/** * @var array */ protected $childTypes = [ // other types ... self::MERCHANT_TYPE => Merchant::class, ];
- 在
app/Models/Helpers/UserHelper.php中添加类型辅助函数的检查
/** * Determine whether the user type is merchant. * * @return bool */ public function isMerchant() { return $this->type == User::MERCHANT_TYPE; }
- 在
database/seeders/UserSeeder.php中添加种子文件
Merchant::factory()->count(10)->create();
- 将翻译语言文件名添加到
config/lang-generator.php
'lang' => [ // ... 'merchants' => base_path('lang/{lang}/merchants.php'), /* The lang of generated crud will set here: Don't remove this line */ ],
- 更新生成的类型
lang/ar/merchants.php中的阿拉伯语翻译 - 将类型翻译键添加到
lang/{lang}/users.php
'types' => [ // Other types ... 'merchant' => 'Merchant', ],
- 从
customer目录克隆仪表板中的视图文件到merchant并将所有customer单词替换为merchantCustomer=>Merchantcustomers=>merchantscustomer=>merchant
- 在
resources/views/dashboard/accounts/sidebar.blade.php中添加侧边栏链接
[
'name' => trans('merchants.plural'),
'url' => route('dashboard.merchants.index'),
'can' => ['ability' => 'viewAny', 'model' => \App\Models\Merchant::class],
'active' => request()->routeIs('*merchants*'),
],
- 在
routes/dashboard.php文件中添加新类型的路由
// Merchants Routes. Route::get('trashed/merchants', 'MerchantController@trashed')->name('merchants.trashed'); Route::get('trashed/merchants/{trashed_merchant}', 'MerchantController@showTrashed')->name('merchants.trashed.show'); Route::post('merchants/{trashed_merchant}/restore', 'MerchantController@restore')->name('merchants.restore'); Route::delete('merchants/{trashed_merchant}/forceDelete', 'MerchantController@forceDelete')->name('merchants.forceDelete'); Route::resource('merchants', 'MerchantController');
- 在
storage/soft_deletes_route_binding.json中添加路由绑定
{
"trashed_merchant": "App\\Models\\Merchant"
}
- 在
storage/permissions.json中添加权限
[
"manage.merchants"
]
- 将
actingAsMerchant辅助函数添加到tests/TestCase.php
/** * Set the currently logged in merchant for the application. * * @param null $driver * @return \App\Models\Merchant */ public function actingAsMerchant($driver = null) { $merchant = Merchant::factory()->create(); $this->be($merchant, $driver); return $merchant; }