laravel-admin利用ModelTree實現(xiàn)對分類信息的管理
更新時間:2020年01月18日 15:11:58 作者:zhiqiang
這篇文章主要介紹了laravel-admin利用ModelTree實現(xiàn)對分類信息的管理,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
生成模型和遷移文件
php artisan make:model Models/Shoping/Category -m
app/Models/Shoping/Category.php
<?php namespace App\Models\Shoping; use Encore\Admin\Traits\AdminBuilder; use Encore\Admin\Traits\ModelTree; use Illuminate\Database\Eloquent\Model; /** * * Class Category * @package App\Models\Shoping */ class Category extends Model { // use ModelTree, AdminBuilder; protected $table="shoping_categories"; public function __construct(array $attributes = []) { parent::__construct($attributes); $this->setTitleColumn("name"); } }
遷移文件
class CreateCategoriesTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('shoping_categories', function (Blueprint $table) { $table->increments('id'); $table->integer('parent_id')->unsigned()->nullable(); $table->string('name'); $table->string('description')->nullable(); $table->integer('order')->unsigned(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('shoping_categories'); } }
生成控制器
php artisan admin:make CategoriesController --model=App\Models\Shoping\Category
app/Admin/Controllers/CategoriesController.php
use App\Models\Shoping\Category; use Encore\Admin\Controllers\AdminController; use Encore\Admin\Form; use Encore\Admin\Grid; use Encore\Admin\Layout\Column; use Encore\Admin\Layout\Content; use Encore\Admin\Layout\Row; use Encore\Admin\Show; use Encore\Admin\Tree; use Encore\Admin\Widgets\Box; class CategoriesController extends AdminController { public function index(Content $content) { return $content->title($this->title) ->description("分類列表") ->row(function (Row $row) { $row->column(6, $this->treeView()->render()); $row->column(6, function (Column $column) { $form = new Form(); $form->select('parent_id', "父類名稱")->options(Category::selectOptions()); $form->text('name', __('Name')); $form->text('description', __('Description')); $form->number('order', '排序序號')->default(0); $column->append((new Box(trans('admin.new'), $form))->style('success')); }); }); } protected function treeView() { return Category::tree(function (Tree $tree) { $tree->disableCreate(); return $tree; }); }
添加路由
app/admin/routes.php
$router->resource('categories',CategoryController::class);
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關文章
深入剖析瀏覽器退出之后php還會繼續(xù)執(zhí)行么
覽器退出之后php還會繼續(xù)執(zhí)行么?下面小編就為大家介紹一下究竟覽器退出之后php還會不會繼續(xù)執(zhí)行。一起跟隨小編過來看看吧2016-05-05PHP連接SQL Server的方法分析【基于thinkPHP5.1框架】
這篇文章主要介紹了PHP連接SQL Server的方法,結合實例形式分析了基于thinkPHP5.1框架Db類以及使用PDO進行SQL Server數(shù)據(jù)庫連接的相關操作實現(xiàn)技巧,需要的朋友可以參考下2019-05-05PHP的MVC模式實現(xiàn)原理分析(一相簡單的MVC框架范例)
PHP的mvc框架很多,像Yii,CodeIgniter,ThinkPHP等現(xiàn)在流行的框架,利用MVC模式進行web頁面的開發(fā),我們可以非常方便的編寫web程序2014-04-04PHP 進程池與輪詢調(diào)度算法實現(xiàn)多任務的示例代碼
這篇文章主要介紹了PHP 進程池與輪詢調(diào)度算法實現(xiàn)多任務的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11php模仿qq空間或朋友圈發(fā)布動態(tài)、評論動態(tài)、回復評論、刪除動態(tài)或評論的功能(中)
這篇文章主要介紹了模仿qq空間或朋友圈發(fā)布動態(tài)、評論動態(tài)、回復評論、刪除動態(tài)或評論的功能(中) ,需要的朋友可以參考下2017-06-06