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

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 关于大棚监测的pcb板设计
  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计