【问题标题】:Laravel Eloquent model update according to condition of field value null or existingLaravel Eloquent 模型根据字段值为空或存在的条件更新
【发布时间】:2017-11-06 19:54:33
【问题描述】:

我正在尝试像这样更新 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 . "')")
                    ]);
    

    我的错误是什么?我怎样才能使陈述更简单?请告诉我~

【问题讨论】:

    标签: php laravel conditional-statements concat


    【解决方案1】:

    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, "\r\n' . $notesAdd . '")'),
        ]);
    

    【讨论】:

    • 谢谢!是否可以将 2 个查询合并为一个?
    • 不,你不能,但你可以玩转,将通用条件分配给一个变量,然后为每个查询克隆它并为每个查询添加自定义条件
    • 谢谢@Aboudeh87
    猜你喜欢
    • 1970-01-01
    • 2022-10-24
    • 1970-01-01
    • 2017-09-19
    • 2021-09-07
    • 2013-01-30
    • 1970-01-01
    • 1970-01-01
    • 2019-05-30
    相关资源
    最近更新 更多