• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

laravel-admin自动生成模块,及相关基础配置方法

php 搞代码 4年前 (2022-01-01) 41次浏览 已收录 0个评论

今天小编就为大家分享一篇laravel-admin自动生成模块,及相关基础配置方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

一、模型创建、数据迁移、以及关联模型控制器

 $ php artisan make:model Brand -m  //创建模型并生成迁移文件 $ php artisan migrate  //运行迁移 $ php artisan admin:make BrandController --model=App\Brand  //创建关联Brand模型的控制器

二、问题:创建模型后,会生成一个临时文件(php artisan make:model Brand -m)

路径:database/migrations/2018_10_16_0000_create_模型名s_table.php

在up方法中加入数据表应有的字段,例如:

 $table->text('content');

可用的字段类型

数据库结构生成器包含构建表时可以指定的各种字段类型:

命令 描述
$table->bigIncrements('id'); 递增 ID(主键),相当于「UNSIGNED BIG INTEGER」
$table->bigInteger('votes'); 相当于 BIGINT
$table->binary('data'); 相当于 BLOB
$table->boolean('confirmed'); 相当于 BOOLEAN
$table->char('name', 4); 相当于带有长度的 CHAR
$table->date('created_at'); 相当于 DATE
$table->dateTi来源gao*daima.com搞@代#码网me('created_at'); 相当于 DATETIME
$table->dateTimeTz('created_at'); 相当于带时区 DATETIME
$table->decimal('amount', 8, 2); 相当于带有精度与基数 DECIMAL
$table->double('column', 8, 2); 相当于带有精度与基数 DOUBLE
$table->enum('level', ['easy', 'hard']); 相当于 ENUM
$table->float('amount', 8, 2); 相当于带有精度与基数 FLOAT
$table->geometry('positions'); 相当于 GEOMETRY
$table->geometryCollection('positions'); 相当于 GEOMETRYCOLLECTION
$table->increments('id'); 递增的 ID (主键),相当于「UNSIGNED INTEGER」
$table->integer('votes'); 相当于 INTEGER
$table->ipAddress('visitor'); 相当于 IP 地址
$table->json('options'); 相当于 JSON
$table->jsonb('options'); 相当于 JSONB
$table->lineString('positions'); 相当于 LINESTRING
$table->longText('description'); 相当于 LONGTEXT
$table->macAddress('device'); 相当于 MAC 地址
$table->mediumIncrements('id'); 递增 ID (主键) ,相当于「UNSIGNED MEDIUM INTEGER」
$table->mediumInteger('votes'); 相当于 MEDIUMINT
$table->mediumText('description'); 相当于 MEDIUMTEXT
$table->morphs('taggable'); 相当于加入递增的 taggable_id 与字符串 taggable_type
$table->multiLineString('positions'); 相当于 MULTILINESTRING
$table->multiPoint('positions'); 相当于 MULTIPOINT
$table->multiPolygon('positions'); 相当于 MULTIPOLYGON
$table->nullableMorphs('taggable'); 相当于可空版本的 morphs() 字段
$table->nullableTimestamps(); 相当于可空版本的 timestamps() 字段
$table->point('position'); 相当于 POINT
$table->polygon('positions'); 相当于 POLYGON
$table->rememberToken(); 相当于可空版本的 VARCHAR(100) 的 remember_token 字段
$table->smallIncrements('id'); 递增 ID (主键) ,相当于「UNSIGNED SMALL INTEGER」
$table->smallInteger('votes'); 相当于 SMALLINT
$table->softDeletes(); 相当于为软删除添加一个可空的 deleted_at 字段
$table->softDeletesTz(); 相当于为软删除添加一个可空的 带时区的 deleted_at 字段
$table->string('name', 100); 相当于带长度的 VARCHAR
$table->text('description'); 相当于 TEXT
$table->time('sunrise'); 相当于 TIME
$table->timeTz('sunrise'); 相当于带时区的 TIME
$table->timestamp('added_on'); 相当于 TIMESTAMP
$table->timestampTz('added_on'); 相当于带时区的 TIMESTAMP
$table->tinyIncrements('id'); 相当于自动递增 UNSIGNED TINYINT
$table->tinyInteger('votes'); 相当于 TINYINT
$table->unsignedBigInteger('votes'); 相当于 Unsigned BIGINT
$table->unsignedDecimal('amount', 8, 2); 相当于带有精度和基数的 UNSIGNED DECIMAL
$table->unsignedInteger('votes'); 相当于 Unsigned INT
$table->unsignedMediumInteger('votes'); 相当于 Unsigned MEDIUMINT
$table->unsignedSmallInteger('votes'); 相当于 Unsigned SMALLINT
$table->unsignedTinyInteger('votes'); 相当于 Unsigned TINYINT
$table->uuid('id'); 相当于 UUID
$table->year('birth_year'); 相当于 YEAR

三、然后运行迁移和创建关联的控制器

 $ php artisan migrate  //运行迁移 $ php artisan admin:make BrandController --model=App\Brand  //创建关联Brand模型的控制器 

四、如果数据库表结构需要修改

如二步骤,修改完成,删除migrations表中相关的那条记录,并且删除相关表

再次运行迁移,此方法适用于无数据的表,已有数据库的表,请不要操作

 $ php artisan migrate  //运行迁移

五、新创建的后台模块与后台自带的模块不一样

新创建:

后他自带:

如果想改成后台自带这种样式的,就必须得调用系统自带的方法,如下:

1,先在需要添加的控制器中引入这些类:

 use Encore\Admin\Show; use Encore\Admin\Tree; use Encore\Admin\Layout\Row; use Encore\Admin\Widgets\Box; use Encore\Admin\Facades\Admin; use Encore\Admin\Layout\Column;

2,并且在index方法中,所有的替换成如下代码:

 return Admin::content(function (Content $content) { $content->header('Index'); $content->description('description'); $content->row(function (Row $row) { $row->column(6, $this->treeView()->render()); $row->column(6, function (Column $column) { $form = new \Encore\Admin\Widgets\Form(); $form->action(admin_base_path('/cate控制器名'));//控制器名 $form->select('fid','父级栏目')->options(Cate控制器名::selectOptions());//控制器名 $form->text('name','栏目名称')->rules('required');//其他form根据情况自行修改 $form->text('sort','排序')->rules('required'); $form->text('jump_to','跳转')->rules('required');; $form->hidden('_token')->default(csrf_token()); $column->append((new Box(trans('admin.new'), $form))->style('success')); }); }); });

3,而且要在此控制器中添加如下方法:

 /** * Make a treeView() * * @return tree */ protected function treeView() { return Cate控制器名::tree(function (Tree $tree) { $tree->disableCreate(); return $tree; }); }

4,在相关的model中添加如下方法和引用类:

 //引用这两个类 use Encore\Admin\Traits\AdminBuilder; use Encore\Admin\Traits\ModelTree; //并且添加这个方法 use ModelTree, AdminBuilder; //字段自行修改 protected $fillable = ['name','sort','fid','jump_to']; public function __construct(array $attributes = []) { parent::__construct($attributes); $this->setParentColumn('fid'); $this->setOrderColumn('sort'); $this->setTitleColumn('name'); }

以上就是laravel-admin自动生成模块,及相关基础配置方法的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:laravel-admin自动生成模块,及相关基础配置方法

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址