laravel-admin自動生成模塊,及相關(guān)基礎(chǔ)配置方法
一、模型創(chuàng)建、數(shù)據(jù)遷移、以及關(guān)聯(lián)模型控制器
$ php artisan make:model Brand -m //創(chuàng)建模型并生成遷移文件 $ php artisan migrate //運行遷移 $ php artisan admin:make BrandController --model=App\Brand //創(chuàng)建關(guān)聯(lián)Brand模型的控制器
二、問題:創(chuàng)建模型后,會生成一個臨時文件(php artisan make:model Brand -m)
路徑:database/migrations/2018_10_16_0000_create_模型名s_table.php
在up方法中加入數(shù)據(jù)表應(yīng)有的字段,例如:
$table->text('content');
可用的字段類型
數(shù)據(jù)庫結(jié)構(gòu)生成器包含構(gòu)建表時可以指定的各種字段類型:
命令 | 描述 |
---|---|
$table->bigIncrements('id'); |
遞增 ID(主鍵),相當(dāng)于「UNSIGNED BIG INTEGER」 |
$table->bigInteger('votes'); |
相當(dāng)于 BIGINT |
$table->binary('data'); |
相當(dāng)于 BLOB |
$table->boolean('confirmed'); |
相當(dāng)于 BOOLEAN |
$table->char('name', 4); |
相當(dāng)于帶有長度的 CHAR |
$table->date('created_at'); |
相當(dāng)于 DATE |
$table->dateTime('created_at'); |
相當(dāng)于 DATETIME |
$table->dateTimeTz('created_at'); |
相當(dāng)于帶時區(qū) DATETIME |
$table->decimal('amount', 8, 2); |
相當(dāng)于帶有精度與基數(shù) DECIMAL |
$table->double('column', 8, 2); |
相當(dāng)于帶有精度與基數(shù) DOUBLE |
$table->enum('level', ['easy', 'hard']); |
相當(dāng)于 ENUM |
$table->float('amount', 8, 2); |
相當(dāng)于帶有精度與基數(shù) FLOAT |
$table->geometry('positions'); |
相當(dāng)于 GEOMETRY |
$table->geometryCollection('positions'); |
相當(dāng)于 GEOMETRYCOLLECTION |
$table->increments('id'); |
遞增的 ID (主鍵),相當(dāng)于「UNSIGNED INTEGER」 |
$table->integer('votes'); |
相當(dāng)于 INTEGER |
$table->ipAddress('visitor'); |
相當(dāng)于 IP 地址 |
$table->json('options'); |
相當(dāng)于 JSON |
$table->jsonb('options'); |
相當(dāng)于 JSONB |
$table->lineString('positions'); |
相當(dāng)于 LINESTRING |
$table->longText('description'); |
相當(dāng)于 LONGTEXT |
$table->macAddress('device'); |
相當(dāng)于 MAC 地址 |
$table->mediumIncrements('id'); |
遞增 ID (主鍵) ,相當(dāng)于「UNSIGNED MEDIUM INTEGER」 |
$table->mediumInteger('votes'); |
相當(dāng)于 MEDIUMINT |
$table->mediumText('description'); |
相當(dāng)于 MEDIUMTEXT |
$table->morphs('taggable'); |
相當(dāng)于加入遞增的 taggable_id 與字符串 taggable_type |
$table->multiLineString('positions'); |
相當(dāng)于 MULTILINESTRING |
$table->multiPoint('positions'); |
相當(dāng)于 MULTIPOINT |
$table->multiPolygon('positions'); |
相當(dāng)于 MULTIPOLYGON |
$table->nullableMorphs('taggable'); |
相當(dāng)于可空版本的 morphs() 字段 |
$table->nullableTimestamps(); |
相當(dāng)于可空版本的 timestamps() 字段 |
$table->point('position'); |
相當(dāng)于 POINT |
$table->polygon('positions'); |
相當(dāng)于 POLYGON |
$table->rememberToken(); |
相當(dāng)于可空版本的 VARCHAR(100) 的 remember_token 字段 |
$table->smallIncrements('id'); |
遞增 ID (主鍵) ,相當(dāng)于「UNSIGNED SMALL INTEGER」 |
$table->smallInteger('votes'); |
相當(dāng)于 SMALLINT |
$table->softDeletes(); |
相當(dāng)于為軟刪除添加一個可空的 deleted_at 字段 |
$table->softDeletesTz(); |
相當(dāng)于為軟刪除添加一個可空的 帶時區(qū)的 deleted_at 字段 |
$table->string('name', 100); |
相當(dāng)于帶長度的 VARCHAR |
$table->text('description'); |
相當(dāng)于 TEXT |
$table->time('sunrise'); |
相當(dāng)于 TIME |
$table->timeTz('sunrise'); |
相當(dāng)于帶時區(qū)的 TIME |
$table->timestamp('added_on'); |
相當(dāng)于 TIMESTAMP |
$table->timestampTz('added_on'); |
相當(dāng)于帶時區(qū)的 TIMESTAMP |
$table->tinyIncrements('id'); |
相當(dāng)于自動遞增 UNSIGNED TINYINT |
$table->tinyInteger('votes'); |
相當(dāng)于 TINYINT |
$table->unsignedBigInteger('votes'); |
相當(dāng)于 Unsigned BIGINT |
$table->unsignedDecimal('amount', 8, 2); |
相當(dāng)于帶有精度和基數(shù)的 UNSIGNED DECIMAL |
$table->unsignedInteger('votes'); |
相當(dāng)于 Unsigned INT |
$table->unsignedMediumInteger('votes'); |
相當(dāng)于 Unsigned MEDIUMINT |
$table->unsignedSmallInteger('votes'); |
相當(dāng)于 Unsigned SMALLINT |
$table->unsignedTinyInteger('votes'); |
相當(dāng)于 Unsigned TINYINT |
$table->uuid('id'); |
相當(dāng)于 UUID |
$table->year('birth_year'); |
相當(dāng)于 YEAR |
三、然后運行遷移和創(chuàng)建關(guān)聯(lián)的控制器
$ php artisan migrate //運行遷移 $ php artisan admin:make BrandController --model=App\Brand //創(chuàng)建關(guān)聯(lián)Brand模型的控制器
四、如果數(shù)據(jù)庫表結(jié)構(gòu)需要修改
如二步驟,修改完成,刪除migrations表中相關(guān)的那條記錄,并且刪除相關(guān)表
再次運行遷移,此方法適用于無數(shù)據(jù)的表,已有數(shù)據(jù)庫的表,請不要操作
$ php artisan migrate //運行遷移
五、新創(chuàng)建的后臺模塊與后臺自帶的模塊不一樣
新創(chuàng)建:
后他自帶:
如果想改成后臺自帶這種樣式的,就必須得調(diào)用系統(tǒng)自帶的方法,如下:
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根據(jù)情況自行修改 $form->text('sort','排序')->rules('required'); $form->text('jump_to','跳轉(zhuǎn)')->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,在相關(guān)的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自動生成模塊,及相關(guān)基礎(chǔ)配置方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
thinkphp5框架實現(xiàn)的自定義擴展類操作示例
這篇文章主要介紹了thinkphp5框架實現(xiàn)的自定義擴展類操作,結(jié)合實例形式簡單分析了thinkPHP5在extend目錄下建立自定義擴展類的具體操作步驟與相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2019-05-05laravel數(shù)據(jù)庫查詢結(jié)果自動轉(zhuǎn)數(shù)組修改實例
這篇文章主要介紹了laravel數(shù)據(jù)庫查詢結(jié)果自動轉(zhuǎn)數(shù)組修改實例,有需要的同學(xué)可以借鑒參考下2021-02-02Laravel學(xué)習(xí)基礎(chǔ)之migrate的使用教程
這篇文章主要給大家介紹了關(guān)于Laravel學(xué)習(xí)基礎(chǔ)之migrate使用的相關(guān)資料,文中通過示例代碼介紹非常詳細,分別介紹了生成遷移、遷移結(jié)構(gòu)、運行遷移和回滾遷移等實現(xiàn)的方法,需要的朋友可以參考借鑒,下面來一起看看吧。2017-10-10ThinkPHP5框架實現(xiàn)簡單的批量查詢功能示例
這篇文章主要介紹了ThinkPHP5框架實現(xiàn)簡單的批量查詢功能,結(jié)合實例形式分析了thinkPHP5框架使用原生SQL語句查詢、批量查詢、快速查詢、聚合查詢等操作實現(xiàn)技巧,需要的朋友可以參考下2018-06-06