2

I am trying to insert an array with date field - end_date in postgresql with Laravel 5.7. But this gives an error -

SQLSTATE[23502]: Not null violation: 7 ERROR:  null value in column \"end_date\" violates not-null constraint

I am using Laravel 5.7, Postgresql 9.3

$array = [
  "amazon_store_id" => 4
  "advertising_profile_id" => 1
  "campaign_id" => 123
  "name" => "6 shelf 2"
  "campaign_type" => "sponsoredProducts"
  "targeting_type" => "manual"
  "premium_bid_adjustment" => true
  "daily_budget" => 15.0
  "start_date" => "2014-11-25 09:32:18"
  "end_date" => null
  "state" => "paused"
  "serving_status" => "CAMPAIGN_PAUSED"
  "creation_date" => "2014-11-07 10:17:03"
  "last_updated_date" => "2018-10-24 12:49:54"
  "created_at" => "2018-12-24 09:32:18"
  "updated_at" => "2018-12-24 09:32:18"
];
DB::table($table_name)->insert($array->toArray());

Ideally it shall insert the null in the database.

2 Answers 2

1

In your migration you can do this to make the column nullable:

public function up()
{
    Schema::create('tablename', function (Blueprint $table) {
        $table->dateTime('end_date')->nullable();
    });
}

->nullable() Designate that the column allows NULL values

Sign up to request clarification or add additional context in comments.

Comments

0

In your migration file you have to define the date field as nullable.

Schema::table($table_name, function (Blueprint $table) {
    $table->dateTime('end_date')->nullable();
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.