您的位置:首页 > 编程语言 > PHP开发

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

2019-10-08 14:56 1636 查看

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

$ 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->dateTime('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自动生成模块,及相关基础配置方法就是小编分享给大家的全部内容了,希望能给大家一个参考

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐