Обновление модели Laravel Eloquent в соответствии с состоянием значения поля null или существующего

Я пытаюсь обновить модель Laravel Eloquent следующим образом.

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 . "')")
]);
});
});

Но это не работает.

Другими словами, я хочу обновить, как показано ниже.

  • если ‘причина’ является нулем или пустой строкой

    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
    ]);
    
  • еще

    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 . "')")
    ]);
    

    В чем моя ошибка? И как я могу сделать заявления проще? Пожалуйста, дайте мне знать ~

0

Решение

Неправильно использовать update функция внутри обратного вызова where функция

Вы должны сделать это в 2 запроса, как:

Res_Reservations::where('time_id', $time['id'])
->where('date', $bus['date'])
->where('valid', config('config.TYPE_SCHEDULE_UNREMOVED'))
->where(function ($query
{
$query->where('reason', null)
->orWhere('reason', '');
})
->update([
'time_id' => $time['move'],
'reason'  => $notesAdd,
]);Res_Reservations::where('time_id', $time['id'])
->where('date', $bus['date'])
->where('valid', config('config.TYPE_SCHEDULE_UNREMOVED'))
->where('reason', '!=',  null)
->where('reason', '!=' '');
->update([
'time_id' => $time['move'],
'reason'  => DB::raw('CONCAT(reason, "\r\n' . $notesAdd . '")'),
]);
1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector