新聞中心
下面由Laravel教程欄目給大家介紹Laravel如何實(shí)現(xiàn)無限極分類,希望對(duì)需要的朋友有所幫助!
最近開發(fā)商品功能,在嘗試遞歸和引用方式后,驀然回首,突然發(fā)現(xiàn)laravel框架有更簡(jiǎn)單高效的實(shí)現(xiàn)方式,無限極分類實(shí)踐,open code與大家共享!感興趣的Mark一下,謝謝~
表結(jié)構(gòu)如下: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 '父級(jí)id', `level` tinyint(3) unsigned DEFAULT '1' COMMENT '分類等級(jí)', `status` tinyint(3) unsigned DEFAULT '0' COMMENT '分類狀態(tài):0-禁用,1-正常', `created_at` timestamp NULL DEFAULT NULL COMMENT '創(chuàng)建時(shí)間', `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新時(shí)間', PRIMARY KEY (`id`) USING BTREE, KEY `status` (`status`)) ENGINE=InnoDB AUTO_INCREMENT=32 DEFAULT CHARSET=utf8mb4 COMMENT='商品分類表';數(shù)據(jù)存儲(chǔ)格式:業(yè)務(wù)代碼:
// 模型文件 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);處理后數(shù)據(jù):
至此,laravel框架無限極分類實(shí)現(xiàn)完畢,相比遞歸和引用實(shí)現(xiàn)無限極分類的兩種方式,是不是簡(jiǎn)單高效很多呢,關(guān)于更多l(xiāng)aravel特性,歡迎評(píng)論區(qū)留言探討。
網(wǎng)站標(biāo)題:Laravel如何實(shí)現(xiàn)無限極分類
文章出自:http://www.ef60e0e.cn/article/cgjhsc.html