我正在尝试更新Laravel雄辩的模型。
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, rn'" . $notesAdd . "')")
]);
});
});
但是它不起作用。
换句话说,我想如下更新。
如果'原因'为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 ]);
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, 'rn" . $notesAdd . "')") ]);
我的错误是什么?如何使语句更简单?请让我知道〜
在where
功能的回调中使用update
函数是错误的
您必须在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, "rn' . $notesAdd . '")'),
]);