我正在尝试像这样更新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 . "')")
]);
});
});
但它不起作用。
换句话说,我想更新如下。
如果&#39;原因&#39;是null或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
]);
其他
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 :(得分:1)
在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 . '")'),
]);