更新表自增,有时生效,有时不生效

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');
还望不吝赐教。
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 5

这是sql的问题吧,看看这个 博文

2年前 评论
MuYan (楼主) 2年前
随波逐流

这种问题,跟框架有啥关系?

2年前 评论
MuYan (楼主) 2年前
DonnyLiu

代码中的逻辑有问题,跟laravel关系不大吧

2年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!