更新表自增,有时生效,有时不生效
1. 运行环境
1). 当前使用的 Laravel 版本?
Laravel Framework 8.83.1
2). 当前使用的 php/php-fpm 版本?
PHP 版本:7.4
3). 当前系统
Windows 11
4). 业务环境
开发环境
5). 相关软件版本
artisan命令
2. 问题描述?
<?php
declare(strict_types=1);
namespace App\Console\Commands;
use App\Models\Links\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class GenerateUserCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'guser';
/**
* 素材关联域名(一次性脚本)
*
* @var string
*/
protected $description = 'generate user';
public function handle()
{
User::where('id', '>', 1)->delete();
DB::statement('ALTER TABLE `users` AUTO_INCREMENT = 2');
foreach (['齐', '得', '隆', '咚', '强'] as $item) {
User::create([
'user_name' => $item,
'user_status' => 1,
'user_pass' => bcrypt('123456'),
]);
}
$this->output->text('管理员创建成功');
}
}
3. 您期望得到的结果?
新插入的数据,能从2开始自增
4. 您实际得到的结果?
5. 当把生成的sql放到navicat中执行时,id是自增的。
delete from `users` where `id` > 1;
alter table `users` auto_increment = 2;
insert into `users` (`user_name`, `user_pass`) values ('齐', '$2y$10$ftukdbK5zrUV3mnWTa4XjO/.AqA9EywjRQkaTtE3mCf0Q4OimyJRW');
insert into `users` (`user_name`, `user_pass`) values ('得', '$2y$10$k6QigmVCHRk.7MGJ6dA0me7vRIRXrtcpRk2z0Q1579mtgaIgxZNXi');
insert into `users` (`user_name`, `user_pass`) values ('隆', '$2y$10$5Fr4GbhrFxGlAo/8wGlZI.y/T6Dohr2y/3oFBSz4gxoGnhjg9jNV6');
insert into `users` (`user_name`, `user_pass`) values ('咚', '$2y$10$of17QaifJgqk2iwk1oq/d./a4p2hjzddmcRe7K3K1oRuxEmK12Mxm');
insert into `users` (`user_name`, `user_pass`) values ('强', '$2y$10$Zr3Rxy7KtymqFIpoHjXO2eUwUX37ktx718XjEm3ObS0wy049eIChq');
这是sql的问题吧,看看这个 博文。
这种问题,跟框架有啥关系?
代码中的逻辑有问题,跟laravel关系不大吧