How to reset auto increment in laravel user deletion?

前端 未结 4 629
北恋
北恋 2021-02-05 09:17

I have been struggling to find out a way to reset the auto increment value in Laravel 4 but it seems that this functionality is not embedded in laravel 4 at least for now. so i

4条回答
  •  无人及你
    2021-02-05 09:55

    I don't know if it's smart or not, but this will cleanup your table.

    public function cleanup($table_name)
    {       
        DB::statement("SET @count = 0;");
        DB::statement("UPDATE `$table_name` SET `$table_name`.`id` = @count:= @count + 1;");
        DB::statement("ALTER TABLE `$table_name` AUTO_INCREMENT = 1;");
    }
    

    MySQL will set the AUTO_INCREMENT to last+1
    If you've set your foreign keys to ON UPDATE CASCADE the children will know about the changes and cascade the update.

    This stuff takes server time and gives you little in return. I think that's why you're getting loads of "don't waist your time" responses? For a count you should use ->count() and not the last id.

    I also don't know if the statements should be within a transaction to prevent errors when users are added while your statements are running.

提交回复
热议问题