Home >PHP Framework >Laravel >How does the Laravel framework implement infinite classification?
The following tutorial column of Laravel will introduce to you the method of implementing Infinitus classification in the Laravel framework. I hope it will be helpful to friends in need!
Recently developed product functions, after trying recursion and reference methods, suddenly looking back, I suddenly found that the laravel framework has a simpler and more efficient implementation method, Infinitus classification best practices, Open code to share with everyone! Mark if interested, thank you~
The table structure is as follows:
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='商品分类表';
Data storage format:
## Business code:
// 模型文件 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);
Processing Last data:
At this point, the laravel framework Infinitus classification has been implemented. Compared with the two methods of recursion and reference to implement Infinitus classification, is it much simpler and more efficient? Well, for more laravel features, please leave a message in the comment area to discuss.The above is the detailed content of How does the Laravel framework implement infinite classification?. For more information, please follow other related articles on the PHP Chinese website!