laravel admin實現分類樹/模型樹的示例代碼
更新時間:2020年06月10日 16:39:13 作者:lionvc1
這篇文章主要介紹了laravel admin實現分類樹/模型樹,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
修改模型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 分類樹 模型樹內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
相關文章
laravel實現按月或天或小時統(tǒng)計mysql數據的方法
今天小編就為大家分享一篇laravel實現按月或天或小時統(tǒng)計mysql數據的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
Zend Framework入門教程之Zend_Config組件用法詳解
這篇文章主要介紹了Zend Framework入門教程之Zend_Config組件用法,結合實例形式分析了Zend_Config組件針對各種類型配置文件操作的相關技巧,需要的朋友可以參考下2016-12-12
Laravel 框架基于自帶的用戶系統(tǒng)實現登錄注冊及錯誤處理功能分析
這篇文章主要介紹了Laravel 框架基于自帶的用戶系統(tǒng)實現登錄注冊及錯誤處理功能,結合實例形式分析了laravel框架自帶用戶系統(tǒng)的基本使用方法及操作注意事項,需要的朋友可以參考下2020-04-04

