laravel-admin自動生成模塊,及相關基礎配置方法

今天小編就爲大家分享一篇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->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自動生成模塊,及相關基礎配置方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持神馬文庫。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章