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 did it this way:

$user = User::find($user_id);

                if ($user)  {
                    if ($user->delete()){

                    DB::statement('ALTER TABLE users AUTO_INCREMENT = '.(count(User::all())+1).';');

                    echo json_encode('User Was Deleted Successfully..');
                    }   
            }

each time i delete a user from the database i set the auto increment pointer to the number of all users +1.

如果有人有更好的解决办法,请告诉我..

推荐答案

就像其他人回答的那样,当您删除一行时,实际上没有必要将计数器往回移动.不过,您可以truncate张桌子,其中will delete all the table rows张并重置计数器.

对于应用了Foreign Key Constraints的表,不能使用truncate(truncatedelete不同,delete只是删除所有行,而keeping是自动递增计数器).

Hence, while using foreign key constrains, MySQL might stop you from truncating a table which has foreign key constraints applied to it.

您可以执行以下步骤来实现所需,但请注意,数据完整性可能存在风险.我只在testing purposes岁时用它.

  1. 编辑DatabaseSeeder类(app/database/seeds/DatabaseSeeder.php提供)如下:

    <?php
    class DatabaseSeeder extends Seeder {
        /**
        * Run the database seeds.
        *
        * @return void
        */
        public function run()
        {
            Eloquent::unguard();
    
            // Disable Foreign key check for this connection before running seeders
            DB::statement('SET FOREIGN_KEY_CHECKS=0;');
    
            $this->call('UserTableSeeder');
            // ...
    
            // FOREIGN_KEY_CHECKS is supposed to only apply to a single
            // connection and reset itself but I like to explicitly
            // undo what I've done for clarity
            DB::statement('SET FOREIGN_KEY_CHECKS=1;');
        }
    }
    
  2. 现在,Table Seeder类(本例中为UserTableSeeder,应在app/database/seeds/UserTableSeeder.php处创建)可以按如下方式调用截断表:

    <?php
    class UserTableSeeder extends Seeder {
    
        public function run()
        {
            // Truncate the table.
            DB::table('users')->truncate();
    
    
            // The auto-increment has been reset.
            // Now we can start adding users.
            User::create(
                array(
                    'email' => 'example@domain.com',
                    'password' => Hash::make('test')
                )
            );
        }
    }
    

Laravel相关问答推荐

URL类图中的循环关系

如何将错误从laravel恢复到inertiajs/vue 3应用程序?

我是否需要/是否可以从控制器运行LARLAVEL备份?

使用 App::environment() 与 app()->environment() 与 config('app.env') 之间的区别

所有 POST 请求上的 Laravel 4 CSRF

Laravel 5 控制台( artisan )命令单元测试

指令allow_call_time_pass_reference警告

我在哪里放置事件侦听器和处理程序?

laravel 基本路径

localhost 和stream_socket_enable_crypto():SSL 操作失败,代码为 1

Laravel Passport Route [login] 未定义

使用 laravel eloquent 关系检索除 NULL 之外的所有记录

Laravel Artisan CLI 安全地停止守护进程队列工作者

在 Laravel 的 Homestead 中运行 PHPUnit

Laravel 验证 pdf mime

Laravel:在另一个控制器中加载方法而不更改 url

如何清理 Laravel Bootstrap 缓存配置文件?

如何使用 Laravel 迁移在 Mysql 列中添加注释

Laravel - artisan不工作

Laravel 5.3 - 用户Collection 的单一通知(关注者)