对于Laravel如何实现无限极分类感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解php实现无限极分类,并且为您提供关于BootStrap无限级分类(无限极分类封装版)、laravel使用
对于Laravel如何实现无限极分类感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解php实现无限极分类,并且为您提供关于BootStrap无限级分类(无限极分类封装版)、laravel 使用递归实现无限分类、Laravel 框架实现无限极分类、laravel5.4实现无限级分类的宝贵知识。
本文目录一览:- Laravel如何实现无限极分类(php实现无限极分类)
- BootStrap无限级分类(无限极分类封装版)
- laravel 使用递归实现无限分类
- Laravel 框架实现无限极分类
- laravel5.4实现无限级分类
Laravel如何实现无限极分类(php实现无限极分类)
下面由Laravel教程栏目给大家介绍Laravel如何实现无限极分类,希望对需要的朋友有所帮助!
最近开发商品功能,在尝试递归和引用方式后,蓦然回首,突然发现laravel框架有更简单高效的实现方式,无限极分类最佳实践,open code与大家共享!感兴趣的Mark一下,谢谢~
表结构如下:
CREATE TABLE `goods_category` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键id', `name` varchar(500) DEFAULT '' COMMENT '分类名称', `pid` int(5) unsigned DEFAULT '0' COMMENT '父级id', `level` tinyint(3) unsigned DEFAULT '1' COMMENT '分类等级', `status` tinyint(3) unsigned DEFAULT '0' COMMENT '分类状态:0-禁用,1-正常', `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间', `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间', PRIMARY KEY (`id`) USING BTREE, KEY `status` (`status`)) ENGINE=InnoDB AUTO_INCREMENT=32 DEFAULT CHARSET=utf8mb4 COMMENT='商品分类表';
数据存储格式:
业务代码:
// 模型文件 public function children() { return $this->hasMany(get_class($this), 'pid' ,'id'); } public function allChildren() { return $this->children()->with( 'allChildren' ); }
// 控制器$list = GoodsCategory::with('allChildren')->first();dd($list);
处理后数据:
至此,laravel框架无限极分类实现完毕,相比递归和引用实现无限极分类的两种方式,是不是简单高效很多呢,关于更多laravel特性,欢迎评论区留言探讨。
BootStrap无限级分类(无限极分类封装版)
HTML部分
duoji.js 代码
后端提供的数据类型:json
如果有数据:state=1
例子:
如果没有数据 state=0
例子:
state:"0"
laravel 使用递归实现无限分类
- 实现规格:一个新闻无线分类系统最终实现的效果如下:
ㅣㅡㅡ体育新闻 ㅣㅡㅡㅡㅡ足球新闻 ㅣㅡㅡㅡㅡ篮球新闻 ㅣㅡㅡㅡㅡ其他体育 ㅣㅡㅡ娱乐新闻 ㅣㅡㅡㅡㅡ电影 ㅣㅡㅡㅡㅡ音乐 ㅣㅡㅡ科技新闻 ㅣㅡㅡㅡㅡ智能手机 ㅣㅡㅡㅡㅡㅡㅡ小米手机 ㅣㅡㅡㅡㅡㅡㅡ华为手机
- 数据表结构:
- 数据表记录:
- laravel Model层实现获取分类信息(使用递归)
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Collection;
class Category extends Model
{
protected $table = ''category'';
protected $primaryKey=''CategoryID'';
public $timestamps=false;
//使用递归获取分类 (正式函数)
public function getCategory($sourceItems, $targetItems, $pid=0){
foreach ($sourceItems as $k => $v) {
if($v->pid == $pid){
$targetItems[] = $v;
$this->getCategory($sourceItems, $targetItems, $v->CateID);
}
}
}
//使用递归获取分类信息测试函数 (测试正式函数)
public function getCategoryTest($sourceItems, $targetItems, $pid=0, $str=''ㅣ''){
$str .= ''ㅡㅡ'';
foreach ($sourceItems as $k => $v) {
if($v->pid == $pid){
$v->CateName = $str.$v->CateName;
$targetItems[] = $v;
$this->getCategoryTest($sourceItems, $targetItems, $v->CateID, $str);
}
}
}
//使用递归获取分类信息 (正式函数)
public function getCategoryInfo(){
$sourceItems = $this->get();
$targetItems = new Collection;
$this->getCategory($sourceItems, $targetItems, 0);
return $targetItems;
}
//测试函数 (测试正式函数)
public function getCategoryInfoTest(){
$sourceItems = $this->get();
$targetItems = new Collection;
$this->getCategoryTest($sourceItems, $targetItems);
return $targetItems;
}
}
- laravel ctroller层实现测试效果
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use App\Model\Category;
use Illuminate\Database\Eloquent\Collection;
use DB;
class CategoryCtroller extends Controller
{
public function index(){
$category = new Category;
$items = $category->getCategoryInfoTest();
foreach ($items as $key => $item) {
dump($item->CateName);
}
}
}
- 访问相关的控制器就可看见要实现的无限分类的效果
Laravel 框架实现无限极分类
最近开发商品功能,在尝试递归和引用方式后,蓦然回首,突然发现 laravel 框架有更简单高效的实现方式,无限极分类最佳实践,open code 与大家共享!感兴趣的 Mark 一下,谢谢~
表结构如下:
CREATE TABLE `goods_category` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT ''主键id'',
`name` varchar(500) DEFAULT '''' COMMENT ''分类名称'',
`pid` int(5) unsigned DEFAULT ''0'' COMMENT ''父级id'',
`level` tinyint(3) unsigned DEFAULT ''1'' COMMENT ''分类等级'',
`status` tinyint(3) unsigned DEFAULT ''0'' COMMENT ''分类状态:0-禁用,1-正常'',
`created_at` timestamp NULL DEFAULT NULL COMMENT ''创建时间'',
`updated_at` timestamp NULL DEFAULT NULL COMMENT ''更新时间'',
PRIMARY KEY (`id`) USING BTREE,
KEY `status` (`status`)
) ENGINE=InnoDB AUTO_INCREMENT=32 DEFAULT CHARSET=utf8mb4 COMMENT=''商品分类表'';
数据存储格式:

业务代码:
// 模型文件
public function children() {
return $this->hasMany(get_class($this), ''pid'' ,''id'');
}
public function allChildren() {
return $this->children()->with( ''allChildren'' );
}
// 控制器
$list = GoodsCategory::with(''allChildren'')->first();
dd($list);
处理后数据:

至此,laravel 框架无限极分类实现完毕,相比递归和引用实现无限极分类的两种方式,是不是简单高效很多呢,关于更多 laravel 特性,欢迎评论区留言探讨。
laravel5.4实现无限级分类
1、建立表
php artisan make:migration create_category_table --create=category
在database/migrations/
下找到你的迁移文件
建入:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCategoryTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create(''categorys'', function (Blueprint $table) {
$table->increments(''id'');
$table->integer(''parent_id'');
$table->string(''code'');
$table->string(''name'');
$table->string(''path'');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists(''categorys'');
}
}
php artisan migrate
2、建Model 在app/Category.php
php artisan make: model Category -m
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
public function childCategory() {
return $this->hasMany(''App\Category'', ''parent_id'', ''id'');
}
public function allChildrenCategorys()
{
return $this->childCategory()->with(''allChildrenCategorys'');
}
}
3、调用
$categorys = App/Category::with(''allChildrenCategorys'')->first();
或
$categorys->allChildrenCategorys;
或
$categorys->allChildrenCategorys->first()->allChildrenCategorys;
将树形分类转成数组ID
有朋友问到这个问题我就更新到这了
$arr = [];
array_walk_recursive($categories,function ($v, $k) use(&$arr) {
if($k == ''id'')
$arr[] = $v;
});
今天的关于Laravel如何实现无限极分类和php实现无限极分类的分享已经结束,谢谢您的关注,如果想了解更多关于BootStrap无限级分类(无限极分类封装版)、laravel 使用递归实现无限分类、Laravel 框架实现无限极分类、laravel5.4实现无限级分类的相关知识,请在本站进行查询。
本文标签: