如何重置laravel用户删除中的自动增量? [英] How to reset auto increment in laravel user deletion?

查看:158
本文介绍了如何重置laravel用户删除中的自动增量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力寻找重新设置Laravel 4中的自动增量值的方法,但是似乎至少现在这个功能并没有嵌入到laravel 4中。
所以我这样做:

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

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

DB :: statement('ALTER TABLE users AUTO_INCREMENT =' (计数(用户::所有())+ 1)。 ';');

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

每次我从数据库中删除一个用户我设置自动增加指针到所有用户的数量+1。



如果有人有更好的解决方案通知我..

解决方案

像其他人一样回复,删除一行时,真的不需要将计数器移回。然而,您可以 truncate 一个表,将删除所有表格行并重置计数器。



您不能 truncate 应用了外键约束的表( truncate delete 不同,只需删除所有行,而保持自动递增计数器。 / p>

因此,使用外键限制时,MySQL可能会阻止您截断具有外键约束应用于它。



您可以执行以下步骤来实现您想要的,但请注意,可能会导致数据完整性风险。我只用于我的测试目的


  1. 编辑 DatabaseSeeder class(可在 app / database / seeds / DatabaseSeeder.php 中找到),如下所示:

     <?php 
    class DatabaseSeeder扩展Seeder {
    / **
    *运行数据库种子。
    *
    * @return void
    * /
    public function run()
    {
    Eloquent :: unguard();

    //在运行seeders之前禁用外键检查此连接
    DB :: statement('SET FOREIGN_KEY_CHECKS = 0;');

    $ this-> call('UserTableSeeder');
    // ...

    // FOREIGN_KEY_CHECKS应该只适用于单个
    //连接并重新设置,但我喜欢显式地
    // undo我做了什么澄清
    DB :: statement('SET FOREIGN_KEY_CHECKS = 1;');
    }
    }


  2. 现在Table Seeder类(示例, UserTableSeeder 在这种情况下,应该在 app / database / seeds / UserTableSeeder.php 中创建)可以调用truncate table (s)如下:

     <?php 
    class UserTableSeeder extends Seeder {

    public function run()
    {
    //截断表。
    DB :: table('users') - > truncate();


    //自动增量已重置。
    //现在我们可以开始添加用户了。
    User :: create(
    array(
    'email'=>'example@domain.com',
    'password'=> Hash :: make ')

    );
    }
    }



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.

if anybody has a better solution inform me please..

解决方案

Like everyone else replied, there is not really a need to move the counter back when you delete a row. You can however truncate a table which will delete all the table rows and reset the counter.

You cannot truncate a table that has Foreign Key Constraints applied on it (truncate is not the same as delete which simply deletes all the rows while keeping the auto-increment counter.).

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

You can perform the following steps to achieve what you want, but beware, there may be a risk to your data integrity. I only use it for my testing purposes.

  1. Edit the DatabaseSeeder class (which is available at app/database/seeds/DatabaseSeeder.php) as follows:

    <?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. Now the Table Seeder classes (Example, UserTableSeeder in this case, which should be created at app/database/seeds/UserTableSeeder.php) can call truncate table(s) as follows:

    <?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用户删除中的自动增量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆