I have setup my Laravel migration to allow nullable
dates for my StartTime and EndTime entries as such:
$table->dateTime( 'StartTime' )->nullable();
$table->dateTime( 'EndTime' )->nullable();
When I create a new entry through eloquent, it allows me to insert null
values into my database successfully:
try {
// Create the new Campaign record
$campaign = Campaign::create( $request->all() );
}
+----+-------+--------+-----------+---------+---------------------+
| Id | Name | Active | StartTime | EndTime | created_at |
+----+-------+--------+-----------+---------+---------------------+
| 1 | Test2 | 0 | NULL | NULL | 2020-07-02 22:01:22 |
+----+-------+--------+-----------+---------+---------------------+
1 row in set (0.00 sec)
However, when I later try and update my record using eloquent and still passing a null
value for StartTime, it throws an error:
try {
// Get a reference to the campaign
$campaign = Campaign::find( $id );
// Update the campaign
$campaign->update( $request->all() );
}
(22007) SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: 'null' for column 'StartTime' at row 1
In the case of the create
method, I am not passing in a StartTime value at all, but in the case of the update
method, I am simply passing back the null
value that Laravel returns as part of the model. So in other words, I haven't altered the value of StartTime at all, I've simply just passed $campaign
back to Laravel for the update.
So it seems that Laravel is assigning the nullable()
upon insert of a new entry into the database, but will not allow me to pass a null
value back for the update.
Am I missing something here? I can't seem to find a solution to this anywhere.
UPDATE
Okay, so further investigation seems like my problem is stemming from the AngularJS $http
POST request. For troubleshooting purposes, I added code to my Laravel controller to alter the StartTime to null
:
if( $request->StartTime === 'null' ) {
$request['StartTime'] = null;
}
And that worked. So it looks like Angular is passing the null
value back in the request as 'null'