数据表结构
无限极分类查询
$classifys = Classify::withCount('children',)
->where('user_id',$user->id)
->where('pid',$pid)
->get();
模型
<?php
/**
* Created by Reliese Model.
* Date: Tue, 29 Oct 2019 03:58:39 +0000.
*/
namespace App\Models;
use Reliese\Database\Eloquent\Model as Eloquent;
/**
* Class Classify
*
* @property int $id
* @property int $user_id
* @property int $pid
* @property string $name
* @property \Carbon\Carbon $estimated_time
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property string $deleted_at
*
* @package App\Models
*/
class Classify extends Eloquent
{
use \Illuminate\Database\Eloquent\SoftDeletes;
protected $table = 'classify';
protected $casts = [
'user_id' => 'int',
'pid' => 'int'
];
protected $fillable = [
'user_id',
'pid',
'name',
];
/**
* 获取子模块
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function childrenModule()
{
return $this->hasMany($this, 'pid', 'id');
}
/**
* 递归获取子模块
* @return mixed
*/
public function children()
{
return $this->childrenModule()->with('children');
}
}