Laravel如何實(shí)現(xiàn)無(wú)限極分類

發(fā)布時(shí)間:2024-02-12
下面由laravel教程欄目給大家介紹laravel如何實(shí)現(xiàn)無(wú)限極分類,希望對(duì)需要的朋友有所幫助!
最近開(kāi)發(fā)商品功能,在嘗試遞歸和引用方式后,驀然回首,突然發(fā)現(xiàn)laravel框架有更簡(jiǎn)單高效的實(shí)現(xiàn)方式,無(wú)限極分類最佳實(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框架無(wú)限極分類實(shí)現(xiàn)完畢,相比遞歸和引用實(shí)現(xiàn)無(wú)限極分類的兩種方式,是不是簡(jiǎn)單高效很多呢,關(guān)于更多l(xiāng)aravel特性,歡迎評(píng)論區(qū)留言探討。
上一個(gè):gvt電磁閥
下一個(gè):電腦輸入法在左上角怎么辦(如何讓輸入法在電腦右下角顯示)

一種視覺(jué)導(dǎo)航機(jī)器人的設(shè)計(jì)
風(fēng)林火山win7安裝教程(風(fēng)林火山ghost win7旗艦版)_2
公司注銷流程有哪些
好茶是怎么煉成之揉捻工藝
意大利univer線圈dc
欺騙消費(fèi)者虛假宣傳應(yīng)該如何賠償
非法經(jīng)營(yíng)同類營(yíng)業(yè)罪的量刑標(biāo)準(zhǔn)
美國(guó)ampheonl aerospacerf連接器
厚聲201007J0153T4X今日行情、最新報(bào)價(jià)
茶樹(shù)鮮葉攤放工藝參數(shù)研究初探
十八禁 网站在线观看免费视频_2020av天堂网_一 级 黄 色 片免费网站_绝顶高潮合集Videos