if you don't use values?
$new_records = $business->queue_lanes()->createMany($new);
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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
Please or to participate in this conversation.