【问题标题】:Enum value does not get inserted未插入枚举值
【发布时间】:2022-01-11 17:35:04
【问题描述】:

我有一个名为clients 的表,如下所示:

public function up()
    {
        Schema::create('clients', function (Blueprint $table) {
            $table->id();
            $table->string('user_name')->nullable();
            $table->string('first_name')->nullable();
            $table->string('last_name')->nullable();
            $table->string('phone_number')->unique();
            $table->string('email_address')->nullable()->unique();
            $table->string('password')->nullable();
            $table->enum('registration_process', [0, 1, 2, 3, 4, 5]);
            $table->rememberToken();
            $table->timestamps();
        });
    }

我想插入这样的数据:

public function stepOne(Request $request)
    {
        $validData = $request->validate([
            'phone' => ['required','unique:clients,phone_number']
        ]);

        $client = Client::create([
            'phone_number' => "0".$validData['phone'],
            'registration_process' => 0
        ]);

        return redirect(route('client.login'));
    }

如您所见,我已将 registration_process' 设置为 0,但问题是它返回此错误:

SQLSTATE[01000]:警告:第 1 行的“registration_process”列的 1265 数据被截断

正如this 问题的答案所说,我尝试在mysql.connections 中将strict 设置为database.phpfalse,然后又尝试了一次,这一次,错误消失了,但数据没有被插入,它以某种方式设置为 empty

那么这里出了什么问题?我该如何解决这个问题?


更新:

127.0.0.1/digipayesh/clients/       http://localhost/phpmyadmin/tbl_sql.php?db=digipayesh&table=clients
Your SQL query has been executed successfully.

show create table `clients`



clients CREATE TABLE `clients` (
  `id` bigint(20) unsigne...

【问题讨论】:

  • 在您的数据库上设置查询日志并查看正在传递的内容。检查表的创建方式并确保它具有适当的值。 ENUM 列通常不被视为字符串吗?
  • 考虑到 miken32 所说的,'registration_process' => '0' 有效吗?
  • 您能否将show create table 语句的完整输出发布为文本
  • @ÁlvaroGonzález 是的,但我不知道该怎么做,请你帮帮我,我真的被这个困住了......
  • 模型的$fillable数组中是否有registration_process

标签: php laravel laravel-8


【解决方案1】:

根据the documentation(强调我的):

ENUM 是一个 字符串 对象,其值选自创建表时列规范中明确枚举的允许值列表。 p>

因此,您需要像这样设置您的列:

public function up()
    {
        Schema::create('clients', function (Blueprint $table) {
            $table->id();
            $table->string('user_name')->nullable();
            $table->string('first_name')->nullable();
            $table->string('last_name')->nullable();
            $table->string('phone_number')->unique();
            $table->string('email_address')->nullable()->unique();
            $table->string('password')->nullable();
            $table->enum('registration_process', ["0", "1", "2", "3", "4", "5"]);
            $table->rememberToken();
            $table->timestamps();
        });
    }

然后我会将该列添加到模型的 $casts 数组中,以确保它作为字符串传递到数据库:

class Client extends Model
{
    protected $casts = [
        "registration_process" => "string",
    ];
}

您需要在可能将值视为整数以进行比较的任何地方调整逻辑,例如if ($client->registration_status === 1)...

【讨论】:

  • 我刚刚尝试了你所说的,但仍然得到同样的错误
  • 编辑您的问题以包含SHOW CREATE TABLE clients的输出
  • 我刚刚将它添加为 UPDATE,请检查一下
  • 这不是很有帮助。 mysql 命令行的命令的实际输出是什么?作为文本,而不是图像。
  • 我没有显示命令的正确输出,该字段不可扩展!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
  • 2013-01-21
  • 2015-01-18
相关资源
最近更新 更多