thinkphp模型递归查询

效果图:

thinkphp模型递归查询_第1张图片

查询代码:

use app\model\Menu;

function getMenuList(string $uid)
{
    $list = Menu::where('pid', $uid)->select();
    foreach ($list as $val) {
        $val['children'] = getMenuList($val->uid);
    }
    return $list;
}

function getMenuBelong(string $uid)
{
    $has = [];
    $list = Menu::where('pid', $uid)->select();
    foreach ($list as $val) {
        $has[] = $val->id;
        $next = getMenuBelong($val->uid);
        $has = array_merge($has, $next);
    }
    return $has;
}

模型



namespace app\model;

use think\Model;

class Menu extends Model
{
    // 设置字段信息
    protected $schema = [
        'id' => 'int',
        'menu_title' => 'string',
        'menu_type' => 'int',
        'cate_uid' => 'string',
        'use_cate_uid' => 'int',
        'open_tag' => 'int',
        'uid' => 'string',
        'pid' => 'string',
        'order_sort' => 'int',
        'create_time' => 'int',
    ];
    protected $autoWriteTimestamp = 'create_time';
}

你可能感兴趣的:(php)