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

I have a $new collection which consists of an array, which I collect to allow me to run various filters using the Collection functions.

Using Log:info($new) my final collection prints out the following:

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

Once this is completed I use the following following method to create those records in my QueueLane model:

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

You can see I do this by inserting via a relationship from my $business variable which is just a Business model. The queue_lanes() is the name of my related relationship to the Business model.

When executing this line I get the following error:

[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)

The QueueLane model has a name and position attribute that should be populated but it looks like it isnt; and I can't tell why looking at the $new input array. The only possible cause I can think of is possible the array keys are what are being read as the column names instead of the nested arrays.

I also have the name attribute in my $fillable variable in my QueueLane model.

Edit:
I have tried running $new->each(function ($row, $key) use ($business) and inserting each $row record but this still brings up the same issue.

Second Edit:
My Business.php model

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;
    }

I don't have a protected variable in my Business or QueueLane model.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source