denis-kisel/constructor

: 使用 Laravel-admin 控制器生成模型的 ready 解决方案

v2.4.6 2019-11-05 14:42 UTC

README

这是一个通过模式生成模型和/或 Laravel Admin 控制器的迁移的包

依赖

安装

通过 Composer

$ composer require denis-kisel/constructor

使用

创建带有空迁移的模型

命令: construct:model ModelName

示例
$ php artisan construct:model App\\Models\\Post 
输出
  • 模型: App\Models\Post
  • 迁移
Schema::create('posts', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->timestamps();
});

创建带有字段的模型

命令: construct:model ModelName [options]
选项: {--fields=}
字段签名: name:type:length{extraMethod:paramValue}
多字段和额外方法必须用逗号 , 分隔

示例
$ php artisan construct:model App\\Models\\Post --fields=name:string:50,description:text{nullable},sort:integer{default:0},is_active:boolean{default:1}
输出
  • 模型: App\Models\Post
  • 迁移
Schema::create('posts', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('name', 50);
    $table->text('description')->nullable();
    $table->integer('sort')->default(0);
    $table->boolean('is_active')->default(1);
    $table->timestamps();
});

创建绑定到地区的模型(翻译)

见 Translatable 文档
命令: construct:modelt ModelName [options]
选项: {--fields=}
字段签名: name:type:length{extraMethod:paramValue}[t] 参数 [t] 是可选的,表示 translation 字段
多字段和额外方法必须用逗号 , 分隔

示例
$ php artisan construct:modelt App\\Models\\Post --fields=name:string:50[t],description:text{nullable}[t],sort:integer{default:0},is_active:boolean{default:1}
输出
  • 模型: App\Models\Post, App\Models\PostTranslation
  • 迁移
// For Post
Schema::create('posts', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->integer('sort')->default('0');
    $table->boolean('is_active')->default('1');
    $table->timestamps();
});


// For PostTranslation
Schema::create('post_translations', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->integer('post_id')->unsigned();
    $table->string('locale')->index();
    $table->string('name', 50);
    $table->text('description')->nullable();
    $table->unique(['post_id','locale']);
    $table->timestamps();
});

创建带有基本页面字段的模型

命令: construct:page ModelName [options]

示例
$ php artisan construct:page App\\Models\\Post
输出
  • 模型: App\Models\Post
  • 迁移
Schema::create('pages', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('code')->nullable();
    $table->string('slug')->nullable();
    $table->string('name');
    $table->text('description')->nullable();
    $table->string('title')->nullable();
    $table->string('h1')->nullable();
    $table->text('keywords')->nullable();
    $table->text('meta_description')->nullable();
    $table->integer('sort')->default('0');
    $table->boolean('is_active')->default('1');
    $table->timestamps();
});

创建 Laravel-Admin 控制器

见 Laravel-Admin 文档
命令: construct:admin ModelName {--fields=}
字段签名: name:type:length{extraMethod:paramValue}
多字段和额外方法必须用逗号 , 分隔

示例
$ php artisan construct:admin App\\Models\\Post --fields=name:string:50,description:text{nullable},sort:integer{default:0},is_active:boolean{default:1}
输出
  • Admin 控制器: App\Admin\Controllers\PostController
  • 包含
// Grid
protected function grid()
{
    $grid = new Grid(new Post);

    $grid->model()->orderBy('created_at', 'desc');

    $grid->id(__('admin.id'));
    $grid->name( __('admin.name'))->editable('text');
    $grid->description( __('admin.description'))->editable('text');
    $grid->sort( __('admin.sort'))->editable('text');
    $grid->is_active( __('admin.is_active'))->editable('select', ActiveHelper::editable());

    $grid->created_at(__('admin.created_at'));
    $grid->updated_at(__('admin.updated_at'));

    $grid->actions(function ($actions) {
        $actions->disableView();
    });

    return $grid;
}

// Form
protected function form()
{
    $form = new Form(new Post);

    $form->text('name', __('admin.name'))->required();
    $form->ckeditor('description', __('admin.description'));
    $form->number('sort', __('admin.sort'))->default('0')->required();
    $form->switch('is_active', __('admin.is_active'))->default('1')->required();

    return $form;
}

选项

创建带有字段的模型

选项: {--fields=}
字段签名: name:type:length{extraMethod:paramValue}[t] 参数 [t] 是可选的,表示 translation 字段
多字段和额外方法必须用逗号 , 分隔

示例
$ php artisan construct:model App\\Models\\Post --fields=name:string:50[t],description:text{nullable}[t],sort:integer{default:0},is_active:boolean{default:1}

运行迁移

选项: {--m} (迁移)

示例
$ php artisan construct:model App\\Models\\Post --fields=name:string:50 --m

覆盖已存在的模型或/和控制器

选项: {--i} (忽略)

示例
$ php artisan construct:model App\\Models\\Post --fields=name:string:50 --i

创建带有 Laravel-Admin 控制的模型

选项: {--a} (admin)

示例
$ php artisan construct:model App\\Models\\Post --fields=name:string:50 --a

命令

许可证

此包是开源软件,使用 MIT 许可证许可

联系

开发者: Denis Kisel