修改模型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);
//這里根據自己的字段修改
$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);
總結
到此這篇關于laravel admin實現分類樹/模型樹的示例代碼的文章就介紹到這了,更多相關laravel admin 分類樹 模型樹內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- laravel5.6 框架操作數據 Eloquent ORM用法示例
- Laravel 手動開關 Eloquent 修改器的操作方法
- laravel框架數據庫操作、查詢構建器、Eloquent ORM操作實例分析
- Laravel框架Eloquent ORM新增數據、自定義時間戳及批量賦值用法詳解
- Laravel框架Eloquent ORM簡介、模型建立及查詢數據操作詳解
- Laravel框架Eloquent ORM修改數據操作示例
- Laravel Eloquent分表方法并使用模型關聯的實現
- laravel7學習之無限級分類的最新實現方法
- 如何使用Laravel Eloquent來開發無限極分類