jingcai-php/app/Model/ProvinceCityArea.php

38 lines
886 B
PHP
Executable File

<?php
namespace App\Model;
use App\Model\Traits\TableTrait;
use Dcat\Admin\Traits\ModelTree;
use Illuminate\Database\Eloquent\Model;
class ProvinceCityArea extends Model
{
use TableTrait;
public $timestamps = false;
public static function toTree()
{
$data = ProvinceCityArea::select('*')->whereIn('type', ['province', 'city', 'area'])->get()->toArray();
$tree = self::_tree($data, 0);
return $tree;
}
private static function _tree($array, $parentId = 0)
{
$tree = array();
foreach ($array as $item) {
if ($item['parent_id'] == $parentId) {
$children = self::_tree($array, $item['id']);
if (!empty($children)) {
$item['children'] = $children;
}
$tree[] = $item;
}
}
return $tree;
}
}