Laravel RAW query with IGNORE doesn't work as expected
I have a project for travel agent where I need to insert date that support incomplete date. So there are 3 possible format:
YYYY-MM-DD
YYYY-MM-00
YYYY-00-00
00 means month or day is not set.
In my database (MariaDB v10.1.28) I can run this query and it sets the date as I wish:
UPDATE IGNORE schedule SET departure_date = "2019-00-00" WHERE id = "10" // result: 2019-00-00
I use IGNORE to bypass strict mode in config/database.php. In Laravel, when I tried to run this raw SQL query:
DB::update('UPDATE IGNORE schedule SET departure_date = ? WHERE id = ?', ['2019-00-00', '10']); // result: 0000-00-00
said record will have its departure_date changed to 0000-00-00. Is this a bug? There is a workaround to use 3 tinyInt but changing structure might mess everything else up.
are you changing FLIGHT column or DEPARTURE column.. as it stands it looks like your table is called departure and column flight which doesnt make sense to me.
Schema::create('schedule', function (Blueprint $table) {
$table->increments('id');
$table->date('departure_date')->nullable();
});
for creating new record I replace the 00 in date to 01, store it using Eloquent, then update the newly created record with actual incomplete date.
use Illuminate\Http\Request;
use App\Schedule; // model for table 'schedule'
...
public function update(Request $request)
{
// validation here
....
$actualDate = $request->departure_date;
....
// modifying the date then assign it to '$request'
$request->departure_date = $modifiedDate;
....
$schedule= new Schedule($request);
$schedule->save();
DB::update('UPDATE IGNORE schedule SET departure_date = ? WHERE id = ?', [$actualDate, $schedule->id]);
// result: '0000-00-00'
}
I separate it because I want other column to be inserted in strict mode.