我使用这种结构来存储类别。
删除类别时如何删除所有子嵌套类别?
ID | 姓名 | parent_id |
---|---|---|
1 | 测试 1 | 0 |
2 | 测试 2 | 1 |
3 | 测试 3 | 2 |
4 | 测试 4 | 3 |
5 | 测试 5 | 0 |
class Category extends Model
{
public function children()
{
return $this->hasMany(self::class, 'parent_id', 'id');
}
public function parent()
{
return $this->belongsTo(self::class, 'parent_id');
}
}
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->integer('parent_id');
});