首页 > 其他 > 详细

Laravel 递归

时间:2021-07-18 00:02:16      阅读:29      评论:0      收藏:0      [点我收藏+]

 

效果图

技术分享图片

 

 

 

表:

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‘,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=39 DEFAULT CHARSET=utf8mb4 COMMENT=‘商品分类表‘;

 

 

Laravel模型层:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class GoodsCategory extends Model
{
    //
    public $table = ‘goods_category‘;

    public function children()
    {
        return $this->hasMany(get_class($this), ‘pid‘ ,‘id‘);
    }

    public function allChildren()
    {
        return $this->children()->with( ‘allChildren‘ );
    }
}

 

 

Laravel控制器

 function test(){
        $list = GoodsCategory::with(‘allChildren‘)->first();
        return $list;
    }

 

Laravel 递归

原文:https://www.cnblogs.com/fsp69/p/15025305.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!