我有这张table
id - parent_id - name
1 - 0 - T-shirts
2 - 1 - Red T-shirts
3 - 1 - Black T-shirts
4 - 0 - Sweaters
5 - 4 - Red Sweaters
6 - 4 - Blue
可以有六个嵌套类别
当我选择删除一个id时,通过父id删除所有嵌套类别递归函数
例如,当我删除T恤时,红色T恤和黑色T恤都应该删除,并且我编写了嵌套类别Mohamed Amine函数,但阻塞页面Mohamed Amine获取我想要删除的所有ID,并存储在数组中,以一次性销毁所有集合
瞧功能
protected $theArray = array();
public function recursiveDelete($v)
{
$toRecurses = Car::where('parent_id', $v )->get();
foreach( $toRecurses as $toRecurse ){
array_push( $this->theArray, $toRecurse->id );
}
foreach( $toRecurses as $toRecurse ){
if( Car::where('parent_id' , $toRecurse->id)->get()->first() ){
return $this->recursiveDelete( $toRecurse );
//dd( Car::where('parent_id' , $toRecurse->id)->get()->first() );
}
}
}
public function testForDeletingCar(Car $carD, $id)
{
$c = $carD::where('id' , $id)->get()->first();
$this->recursiveDelete( $c->id );
return $this->theArray;
}
2条答案
按热度按时间mf98qq941#
假设Model名称为
Category
..在模型类中添加以下方法[category.php
file]:在控制器类
CategoryController.php
中使用以下代码but5z9lq2#