Atualização do modelo Laravel Eloquent de acordo com a condição do valor do campo nulo ou existente
Estou tentando atualizar o modelo Laravel Eloquent assim.
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, \r\n'" . $notesAdd . "')")
]);
});
});
Mas isso não funciona.
Em outras palavras, quero atualizar como abaixo.
se 'reason' for nulo ou string vazia
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
]);
outro
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, '\r\n" . $notesAdd . "')")
]);
Qual é o meu erro? E como posso simplificar as declarações? Por favor me avise ~