laravel admin實(shí)現(xiàn)分類樹/模型樹的示例代碼
修改模型Category.php
<?php
namespace App\Admin\Models;
use Encore\Admin\Traits\AdminBuilder;
use Encore\Admin\Traits\ModelTree;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use ModelTree, AdminBuilder;
protected $table = 'category';
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
//這里根據(jù)自己的字段修改
$this->setParentColumn('parent_id');
$this->setOrderColumn('sort');
$this->setTitleColumn('name');
}
}
修改控制文件CategoryController.php
<?php
namespace App\Admin\Controllers;
use App\Admin\Models\Category;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
class CategoryController extends AdminController
{
/**
* Title for current resource.
*
* @var string
*/
protected $title = '商品分類管理';
public function index(Content $content)
{
return Admin::content(function ($content) {
$content->header('商品分類管理');
$content->body(Category::tree(function ($tree) {
$tree->branch(function ($branch) {
$src = config('admin.upload.host') . '/' . $branch['image'];
$logo = "<img src='$src' style='max-width:30px;max-height:30px' class='img'/>";
return "{$branch['id']} - {$branch['name']} $logo";
});
}));
});
}
//下面是自己的代碼
//.......
}
添加路由app/Admin/routes.php
$router->resource('categories',CategoryController::class);
select中使用分類樹
$form->select('parent_id', __('Parent id'))->options(Category::selectOptions())->default(1);
總結(jié)
到此這篇關(guān)于laravel admin實(shí)現(xiàn)分類樹/模型樹的示例代碼的文章就介紹到這了,更多相關(guān)laravel admin 分類樹 模型樹內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- laravel5.6 框架操作數(shù)據(jù) Eloquent ORM用法示例
- Laravel 手動開關(guān) Eloquent 修改器的操作方法
- laravel框架數(shù)據(jù)庫操作、查詢構(gòu)建器、Eloquent ORM操作實(shí)例分析
- Laravel框架Eloquent ORM新增數(shù)據(jù)、自定義時間戳及批量賦值用法詳解
- Laravel框架Eloquent ORM簡介、模型建立及查詢數(shù)據(jù)操作詳解
- Laravel框架Eloquent ORM修改數(shù)據(jù)操作示例
- Laravel Eloquent分表方法并使用模型關(guān)聯(lián)的實(shí)現(xiàn)
- laravel7學(xué)習(xí)之無限級分類的最新實(shí)現(xiàn)方法
- 如何使用Laravel Eloquent來開發(fā)無限極分類
相關(guān)文章
PHP對稱加密算法(DES/AES)類的實(shí)現(xiàn)代碼
本篇文章主要介紹了PHP對稱加密算法(DES/AES)類的實(shí)現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
PHP笛卡爾積實(shí)現(xiàn)原理及代碼實(shí)例
這篇文章主要介紹了PHP笛卡爾積實(shí)現(xiàn)原理及代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-12-12
ThinkPHP6.0如何利用自定義驗(yàn)證規(guī)則規(guī)范的實(shí)現(xiàn)登陸
這篇文章主要介紹了ThinkPHP6.0如何利用自定義驗(yàn)證規(guī)則規(guī)范的實(shí)現(xiàn)登陸,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Yii2.0框架模型添加/修改/刪除數(shù)據(jù)操作示例
這篇文章主要介紹了Yii2.0框架模型添加/修改/刪除數(shù)據(jù)操作,結(jié)合實(shí)例形式分析了Yii2.0使用模型操作數(shù)據(jù)的添加、修改、刪除相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-07-07
yii框架結(jié)合charjs統(tǒng)計上一年與當(dāng)前年數(shù)據(jù)的方法示例
這篇文章主要介紹了yii框架結(jié)合charjs統(tǒng)計上一年與當(dāng)前年數(shù)據(jù)的方法,涉及Yii框架后臺數(shù)據(jù)查詢、前臺交互、日期操作等相關(guān)使用技巧,需要的朋友可以參考下2020-04-04
PHP去掉從word直接粘貼過來的沒有用格式的函數(shù)
通常我們會遇到直接把word內(nèi)的內(nèi)容,直接粘貼到文本編輯器中。這時候會出現(xiàn)在文本編輯器中有一些word內(nèi)的沒用的標(biāo)簽內(nèi)容2012-10-10

