Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

cluel3ss's avatar

Cannot save due to null value even though attribute has been assigned

I have an array that contains a list of records I want to insert into my DB; before doing so I turn it into a collection so I can run some filters/transformations on the data easily. Once this is complete I attempt to insert the said array via a relationship in my Model but keep getting errors stating non null constraint is being met.

This is the code:

$submitted_lanes = collect($lanes);
// strip empty lanes
$submitted_lanes = $submitted_lanes->filter(function ($row, $key) {
    return $row['name'] != null;
});
// remove 'fixed' attr
$submitted_lanes->transform(function ($item, $key) {
    unset($item['fixed']);
    return $item;
});
// create insert collection
$new = $submitted_lanes->filter(function ($row, $key) {
    return $row['id'] == null;
});
$new->transform(function ($item, $key) {
    unset($item['id']);
    return $item;
});

DB::transaction(function() use ($business, $to_delete, $existing, $new) {

    Log:info($new->values());
    $new_records = $business->queue_lanes()->createMany($new->values());
    // $new->each(function ($row, $key) use ($business) {
    //     Log:info($row);
    //     $business->queue_lanes()->create([
    //         'name' => 'test value',
    //         'position' => 10,
    //     ]);
    // });
    
}, 1);

This is the error I get:

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

Business Model

class Business extends Model
{
    protected $fillable = [
        'name',
        // other vlaues addded
    ];

    public function queue_lanes()
    {
        return $this->hasMany('App\QueueLane');
    }

QueueLane Model

class QueueLane extends Model
{

    use SoftDeletes;

    protected $fillable = [
        'name',
        'position',
        'business_id'
    ];

    public function business()
    {
        return $this->belongsTo('App\Business');
    }

You can see in my controller I actually debug the $new->values() , this is what that looks like:

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

As you can see I have all the setup as expected, but I am not sure why I am getting this constraint issue. You can see there is a value associated to name

0 likes
10 replies
Snapey's avatar

if you don't use values?

$new_records = $business->queue_lanes()->createMany($new);
cluel3ss's avatar

@Snapey I added ->values() to ensure its not an issue with an associative array, however dropping this has suggested above still returns the same issue.

Snapey's avatar

@cluel3ss temporarily test with a simple collection

$new = collect(['name'=>11, 'position'=>1]);
$new_records = $business->queue_lanes()->createMany($new);
cluel3ss's avatar

@Snapey When I tried the code above I got the following error:

Illuminate\Database\Eloquent\Relations\HasOneOrMany::::create(): Argument #1 ($attributes) must be of type array, string given, I assumed it is because I am using createMany which expects a multi dimentional array.

When I changed the code to the following:

$new = collect([['name'=>'test value', 'position'=>1], ['name'=>'test value2', 'position'=>2]]);
Log:info($new);
$new_records = $business->queue_lanes()->createMany($new);

This gives me the original I recieved regarding non null values.

cluel3ss's avatar

I also tried changing to the following:

$new = collect(['name'=>11, 'position'=>1]);
$new_records = $business->queue_lanes()->create($new);

This returned the same error:

Illuminate\Database\Eloquent\Relations\HasOneOrMany::create(): Argument #1 ($attributes) must be of type array

Snapey's avatar

@cluel3ss create seems to only accept array so

 $new_records = $business->queue_lanes()->create($new->toArray());

createMany just iterates over the passed collection / array , calling the create function, so is no different to looping on the collection on your side

1 like
cluel3ss's avatar

@Snapey I still get the following error when changing the code like above

SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "name" violates not-null constraint DETAIL: Failing row contains (35, null, null, null, 2022-02-06 01:17:44, 2022-02-06 01:17:44, 1, 4, null). (SQL: insert into "queue_lanes" ("name", "position", "business_id", "updated_at", "created_at") values (?, 1, 4, 2022-02-06 01:17:44, 2022-02-06 01:17:44) 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

Snapey's avatar

@cluel3ss something interfering with it in the model?

any eloquent event listeners?

any mutators or accessors?

did you change the base model at all?

do you have a guarded array?

if you dump an instance of the model, do you see your fillable array?

kostasp's avatar

Hello. I think the problem is that your db does not allow null values for name Not null violation: 7 ERROR: null value in column "name" violates not-null constraint. Also (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)

Either change the migration with: $table->string('name')->nullable();

or use a mutator that changes the null to empty string: public function setNameAttribute($name) { $this->attributes['name'] = is_null($name) ? '' : $name; }

cluel3ss's avatar

@kostasp So the issue is that the array I am passing is not null. Trying to figure why why its complaining that it is. I want to enforce non nullable constraints in my name colum.

Please or to participate in this conversation.