如何重置laravel用户删除中的自动增量? [英] How to reset auto increment in laravel user deletion?
问题描述
所以我这样做:
$ 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可能会阻止您截断具有外键约束
应用于它。
您可以执行以下步骤来实现您想要的,但请注意,可能会导致数据完整性风险。我只用于我的测试目的。
-
编辑
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;');
}
}
-
现在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.
Edit the
DatabaseSeeder
class (which is available atapp/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;'); } }
Now the Table Seeder classes (Example,
UserTableSeeder
in this case, which should be created atapp/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屋!