dtmjqyfz21793 2017-06-05 23:26
浏览 424
已采纳

Laravel Eloquent模型根据字段值null或现有条件更新

I am trying to update Laravel Eloquent model like this.

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

But it does not working.

In other words, I want to update as below.

  • if 'reason' is null or 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, '
    " . $notesAdd . "')")
                    ]);
    

    What is my mistake? And how can I make statements simpler? Please let me know~

  • 写回答

1条回答 默认 最新

  • doupiao9318 2017-06-06 00:26
    关注

    It is wrong to use update function inside the callback of where function

    You have to do it in 2 queries like:

    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, "
    ' . $notesAdd . '")'),
        ]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?