I am trying to update Laravel Eloquent model like this.
Res_Reservations::where('time_id', $time['id'])
->where('date', $bus['date'])
->where('valid', config('config.TYPE_SCHEDULE_UNREMOVED'))
->where(function($query) use($time, $notesAdd) {
$query->whereNull('reason', function($query) use ($time, $notesAdd) {
return $query->update([
'time_id' => $time['move'],
'reason' => $notesAdd
]);
})
->orWhere('reason', '=', '', function($query) use ($time, $notesAdd) {
return $query->update([
'time_id' => $time['move'],
'reason' => $notesAdd
]);
})
->orWhere('reason', '<>', '', function($query) use ($time, $notesAdd) {
return $query->update([
'time_id' => $time['move'],
'reason' => DB::raw("CONCAT(reason,
'" . $notesAdd . "')")
]);
});
});
But it does not working.
In other words, I want to update as below.
-
if 'reason' is null or emptystring
Res_Reservations::where('time_id', $time['id']) ->where('date', $bus['date']) ->where('valid', config('config.TYPE_SCHEDULE_UNREMOVED')) ->update([ 'time_id' => $time['move'], 'reason' => $notesAdd ]);
-
else
Res_Reservations::where('time_id', $time['id']) ->where('date', $bus['date']) ->where('valid', config('config.TYPE_SCHEDULE_UNREMOVED')) ->update([ 'time_id' => $time['move'], 'reason' => DB::raw("CONCAT(reason, ' " . $notesAdd . "')") ]);
What is my mistake? And how can I make statements simpler? Please let me know~