使用 createMany in Laravel returns 填充数组时出现空错误

Using createMany in Laravel returns null error when array used is populated

我有一个 $new collection,它由一个数组组成,我 collect 允许我使用 Collection 函数 运行 各种过滤器.

使用 Log:info($new) 我的最终 collection 打印出以下内容:

local.INFO: {"1":{"name":"11","position":1},"2":{"name":"22","position":2},"3":{"name":"33","position":3}}

完成后,我使用以下方法在我的 QueueLane 模型中创建这些记录:

$new_records = $business->queue_lanes()->createMany($new);

你可以看到我是通过从我的 $business 变量插入一个关系来做到这一点的,它只是一个 Business 模型。 queue_lanes() 是我与 Business 模型相关关系的名称。

执行此行时出现以下错误:

[2022-01-29 06:18:01] local.ERROR: SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "name" violates not-null constraint DETAIL: Failing row contains (19, null, null, null, 2022-01-29 06:18:01, 2022-01-29 06:18:01, 1, 4, null). (SQL: insert into "queue_lanes" ("name", "position", "business_id", "updated_at", "created_at") values (?, 1, 4, 2022-01-29 06:18:01, 2022-01-29 06:18:01) returning "id") {"userId":4,"exception":"[object] (Illuminate\Database\QueryException(code: 23502): SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "name" violates not-null constraint DETAIL: Failing row contains (19, null, null, null, 2022-01-29 06:18:01, 2022-01-29 06:18:01, 1, 4, null). (SQL: insert into "queue_lanes" ("name", "position", "business_id", "updated_at", "created_at") values (?, 1, 4, 2022-01-29 06:18:01, 2022-01-29 06:18:01) returning "id") at /Users/Username/Sites/test-app/vendor/laravel/framework/src/Illuminate/Database/Connection.php:712)

QueueLane 模型有一个 nameposition 属性应该填充,但看起来没有;而且我不知道为什么要查看 $new 输入数组。我能想到的唯一可能原因是数组键可能被读取为列名而不是嵌套数组。

我的 QueueLane 模型中的 $fillable 变量也有 name 属性。

编辑:
我已经尝试 运行ning $new->each(function ($row, $key) use ($business) 并插入每个 $row 记录,但这仍然会带来同样的问题。

第二次编辑:
我的 Business.php 模特

class Business extends Model
{
    protected $fillable = [
        'name',
        'business_type_id',
        'vanity_url',
        'allow_self_check_in',
        'enable_self_check_in',
        'enable_multi_queue',
        'multi_queue_active',
        'accept_new_entrants',
        'enable_shortest_option',
        'default_shortest',
    ];

    protected $casts = [
        'is_active'                 => 'boolean',
        'enable_sms_sender_id'      => 'boolean',
        'enable_self_check_in'      => 'boolean',
        'multi_queue_active'        => 'boolean',
        'allow_self_check_in'       => 'boolean',
        'accept_new_entrants'       => 'boolean',
        'enable_shortest_option'    => 'boolean',
        'default_shortest'          => 'boolean',
    ];
...

QueueLane.php

...
class QueueLane extends Model
{

    use SoftDeletes;

    protected $fillable = [
        'name',
        'min_head_count',
        'max_head_count',
        'position',
        'business_id'
    ];
...
    const DEFAULT_QUEUE = '#Default';

    public function setNameAttribute($value)
    {
        $this->attributes['name'] = $value == 'Default Queue' ? QueueLane::DEFAULT_QUEUE : $this->name;
    }

我的 Business 或 QueueLane 模型中没有受保护的变量。

请检查您的 QueueLane 模型,您的模型中有 setNameAttribute 突变器,它导致将 name 属性值设置为 null 时出现问题。在突变器中 $this->name 将 return 为空。