laravel-admin后台框架基本使用
laravel-admin官方中文文檔:http://laravel-admin.org/docs/#/zh/
laravel-admin的github:https://github.com/z-song/laravel-admin
一、安裝與配置
按照官方文檔或者github的步驟安裝和配置即可。
二、建立控制器
在app/Admin/Controllers新建對應的控制器來管理某個數據表。
控制器例子:
1 <?php 2 3 namespace App\Admin\Controllers; 4 5 use Encore\Admin\Form; 6 use Encore\Admin\Grid; 7 use Encore\Admin\Facades\Admin; 8 use Encore\Admin\Layout\Content; 9 use App\Http\Controllers\Controller; 10 use Encore\Admin\Controllers\ModelForm; 11 12 class ExampleController extends Controller 13 { 14 use ModelForm; 15 16 /** 17 * Index interface. 18 * 19 * @return Content 20 */ 21 public function index() 22 { 23 return Admin::content(function (Content $content) { 24 25 $content->header('header'); 26 $content->description('description'); 27 28 $content->body($this->grid()); 29 }); 30 } 31 32 /** 33 * Edit interface. 34 * 35 * @param $id 36 * @return Content 37 */ 38 public function edit($id) 39 { 40 return Admin::content(function (Content $content) use ($id) { 41 42 $content->header('header'); 43 $content->description('description'); 44 45 $content->body($this->form()->edit($id)); 46 }); 47 } 48 49 /** 50 * Create interface. 51 * 52 * @return Content 53 */ 54 public function create() 55 { 56 return Admin::content(function (Content $content) { 57 58 $content->header('header'); 59 $content->description('description'); 60 61 $content->body($this->form()); 62 }); 63 } 64 65 /** 66 * Make a grid builder. 67 * 68 * @return Grid 69 */ 70 protected function grid() 71 { 72 return Admin::grid(YourModel::class, function (Grid $grid) { 73 74 $grid->id('ID')->sortable(); 75 76 $grid->created_at(); 77 $grid->updated_at(); 78 }); 79 } 80 81 /** 82 * Make a form builder. 83 * 84 * @return Form 85 */ 86 protected function form() 87 { 88 return Admin::form(YourModel::class, function (Form $form) { 89 90 $form->display('id', 'ID'); 91 92 $form->display('created_at', 'Created At'); 93 $form->display('updated_at', 'Updated At'); 94 }); 95 } 96 }
其中最重要的方法是 grid()
和 form()
,分別對應數據列表(表格)和數據表單的內容。
建立控制器后,在 app\Admin\routes.php
加上相應的資源路由:
1 $router->resource('example', ExampleController::class);
至此即可使用 /admin/example
訪問這個管理的數據列表。
三、生成模型表格
1、由於laravel-admin模型表格基於laravel的數據模型,所以首先需要建立數據表的對應數據模型。如:
/*加上 -m 可以同時生成遷移文件(按需)*/ php artisan make:model Models\Investor -m
2、在控制器中 index()
是模型表格頁面的入口,Admin::content
則可以在方法內設置本頁面的內容。header
和 description
是頁面的主、副標題,可按需設置。另外可看到本頁面body的內容由 $this->grid()
提供。
1 public function index() 2 { 3 return Admin::content(function (Content $content) { 4 5 $content->header('頁面主標題'); 6 $content->description('頁面副標題'); 7 8 $content->body($this->grid()); 9 }); 10 }
3、grid()
方法用於構建基於數據模型的數據表格。首先要將 YourModel
改為真實的數據模型,然后就可以使用 $grid
構建表單。
每個$grid
代表一列數據,指向的是數據表字段名,括號內的是本列的標題名稱。若括號內不填標題名稱,則自動使用數據表字段名的字符串代替。
1 protected function grid() 2 { 3 return Admin::grid(YourModel::class, function (Grid $grid) { 4 5 // 直接通過字段名`username`添加列 6 $grid->username('用戶名'); 7 // 效果和上面一樣 8 $grid->column('username', '用戶名'); 9 //若需要經過復雜邏輯,可使用display方法修改輸出 10 $grid->gender('性別')->display(function($data){ 11 $result = ''; 12 $result = YourModel::$genderGroup[$data]; 13 return $result; 14 }); 15 $grid->mobile_phone('手機'); 16 $grid->address('通訊地址'); 17 }); 18 }
4、數據表格最后一列默認是操作列,包含編輯按鈕與刪除按鈕。如果需要自定義操作列,則可以把默認的操作列禁用,再進行自定義。
1 $grid->disableActions();
四、生成模型表單
控制器內的 created()
和 edit()
分別對應着模型表單的新增與編輯操作。並且新增、編輯表單共用控制器內的 form()
進行表單構建。
修改form()來構建表單內容:$form
指向的是表單控件類型(text是文本域,radio是單選框等),方法內第一個參數是數據表字段名,第二個參數是字段顯示名稱。
1 protected function form() 2 { 3 return Admin::form(Investor::class, function (Form $form) { 4 //display僅作顯示對應內容 5 $form->display('id', 'ID'); 6 //文本域 7 $form->text('name','姓名'); 8 //單選框,options方法是單選框內容,格式為key=>value的數組形式。 9 $form->radio('gender','性別')->options(Investor::$genderGroup); 10 //...... 11 }); 12 }
更多的表單組件使用方法請查看http://laravel-admin.org/docs/#/zh/model-form-fields