GVKun编程网logo

Laravel如何实现无限极分类(php实现无限极分类)

17

对于Laravel如何实现无限极分类感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解php实现无限极分类,并且为您提供关于BootStrap无限级分类(无限极分类封装版)、laravel使用

对于Laravel如何实现无限极分类感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解php实现无限极分类,并且为您提供关于BootStrap无限级分类(无限极分类封装版)、laravel 使用递归实现无限分类、Laravel 框架实现无限极分类、laravel5.4实现无限级分类的宝贵知识。

本文目录一览:

Laravel如何实现无限极分类(php实现无限极分类)

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='商品分类表';

数据存储格式:

Laravel框架无限极分类简单实现

业务代码:

    // 模型文件
    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框架无限极分类实现完毕,相比递归和引用实现无限极分类的两种方式,是不是简单高效很多呢,关于更多laravel特性,欢迎评论区留言探讨。

BootStrap无限级分类(无限极分类封装版)

BootStrap无限级分类(无限极分类封装版)

HTML部分

rush:js;"> Meta charset="UTF-8"> 多级联动封装

duoji.js 代码

rush:js;"> (function ($) { $.select=function(Box,addInputClass){ var i=new select; return i.init(Box,addInputClass) } //声明多级联动 方法类 var select = new Function(); select.prototype={ //定义类属性 init:function(Box,addInputClass){ this.BoxName=Box; this.Box=$('#'+Box); //需要添加元素的容器 this.eleClass=addInputClass;//每个事件的定位class this.first();//新建一个select获取 },first:function(){ //声明外部变量 var BoxName=this.BoxName; var eleClass= this.eleClass; var Box=this.Box; var obj=this; $.get("http://127.0.0.1:83/areas",{id:'0'},function(data){ var option=""; var list=data.data; for(var key in list){ option+=""; } $('').appendTo(Box).find('select').bind('change',function(){obj.change($(this))}); },'jsonp'); },//change事件 change:function(event){ //声明 var BoxName=$(event).attr('pnode'); //获取触发事件者的pnode var className=$(event).attr('cname');//获取触发事件者的cname var Box =$("#"+BoxName); //获取所有插入盒子的对象 var eleClass=$("."+className); //获取所有Class所在元素组 var num=eleClass.index($(event))+1; //获取num的值 var id=$(event).val(); //获取ajax发送id的头 var obj=this; //代表这个方法 //清除 后续添加的新的元素 eleClass.each(function(){ //这里的this 代表eleClass 遍历时的单个对象 //console.log($(this).attr('num')); //console.log($().attr('num')); if($(this).attr('num')>$(event).attr('num')){ $(this).parent().remove(); } }); /* console.log(BoxName); console.log(className); console.log($(event)); console.log($(event).val()); */ //循环ajax方法 $.ajax({ type: "get",dataType:"jsonp",url: "http://127.0.0.1:83/areas",data: {id:id},sync: false,//设置为同步 success: function(data){ //console.log(data); var list =data.data if(data.state==='1'){ var option=""; for(var key in list){ option+=""; } $(''+option+'
').appendTo(Box).find('select').bind('change',function(){obj.change(this)}); } } }); },//查询当前盒子中的信息 log:function(){ var BoxName=$(event).attr('pnode'); //获取触发事件者的pnode var className=$(event).attr('cname');//获取触发事件者的cname var Box =$("#"+BoxName); //获取所有插入盒子的对象 var eleClass=$("."+className); //获取所有Class所在元素组 console.log("容器名:"+BoxName+"\n 触发的class名:"+className); },//第一个select框获取信息 getFirstElement:function(){ var main=$('#'+this.main); $.get("http://127.0.0.1:83/areas",function(data){ var option=""; var list=data.data; for(var key in list){ option+=""; } main.html(option); },'jsonp'); } } })(jQuery)

后端提供的数据类型:json

如果有数据:state=1

例子:

rush:js;"> data:{ 110000:{id: "110000",areaname: "北京",pid: "0",shortname: "北京",level: "1",position: "tr_0",sort: "1"} 120000:{id: "120000",areaname: "天津",shortname: "天津",sort: "2"} 130000:{id: "130000",areaname: "河北省",shortname: "河北",sort: "3"} 140000:{id: "140000",areaname: "山西省",shortname: "山西",sort: "4"} 150000:{id: "150000",areaname: "内蒙古自治区",shortname: "内蒙古",sort: "5"} 210000:{id: "210000",areaname: "辽宁省",shortname: "辽宁",sort: "6"} 220000:{id: "220000",areaname: "吉林省",shortname: "吉林",sort: "7"} 230000:{id: "230000",areaname: "黑龙江省",shortname: "黑龙江",sort: "8"} 310000:{id: "310000",areaname: "上海",shortname: "上海",sort: "9"} 320000:{id: "320000",areaname: "江苏省",shortname: "江苏",sort: "10"} 330000:{id: "330000",areaname: "浙江省",shortname: "浙江",sort: "11"} ..... },state:"1"

如果没有数据 state=0

例子:

state:"0"

laravel 使用递归实现无限分类

laravel 使用递归实现无限分类

ㅣㅡㅡ体育新闻
ㅣㅡㅡㅡㅡ足球新闻
ㅣㅡㅡㅡㅡ篮球新闻
ㅣㅡㅡㅡㅡ其他体育
ㅣㅡㅡ娱乐新闻
ㅣㅡㅡㅡㅡ电影
ㅣㅡㅡㅡㅡ音乐
ㅣㅡㅡ科技新闻
ㅣㅡㅡㅡㅡ智能手机
ㅣㅡㅡㅡㅡㅡㅡ小米手机
ㅣㅡㅡㅡㅡㅡㅡ华为手机

<?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;
    }

}
<?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 框架实现无限极分类

最近开发商品功能,在尝试递归和引用方式后,蓦然回首,突然发现 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=''商品分类表'';

数据存储格式:

Laravel框架无限极分类简单实现

业务代码:

// 模型文件
    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 框架无限极分类实现完毕,相比递归和引用实现无限极分类的两种方式,是不是简单高效很多呢,关于更多 laravel 特性,欢迎评论区留言探讨。

 

laravel5.4实现无限级分类

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实现无限级分类的相关知识,请在本站进行查询。

本文标签:

上一篇非常漂亮的 Laravel 管理界面!(laravel 管理系统)

下一篇总结Laravel项目部署到线上需要注意的一些问题(laravel项目部署到服务器)