修改模型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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- laravel5.6 框架操作數(shù)據(jù) Eloquent ORM用法示例
- Laravel 手動(dòng)開關(guān) Eloquent 修改器的操作方法
- laravel框架數(shù)據(jù)庫(kù)操作、查詢構(gòu)建器、Eloquent ORM操作實(shí)例分析
- Laravel框架Eloquent ORM新增數(shù)據(jù)、自定義時(shí)間戳及批量賦值用法詳解
- Laravel框架Eloquent ORM簡(jiǎn)介、模型建立及查詢數(shù)據(jù)操作詳解
- Laravel框架Eloquent ORM修改數(shù)據(jù)操作示例
- Laravel Eloquent分表方法并使用模型關(guān)聯(lián)的實(shí)現(xiàn)
- laravel7學(xué)習(xí)之無限級(jí)分類的最新實(shí)現(xiàn)方法
- 如何使用Laravel Eloquent來開發(fā)無限極分類