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 微带串馈天线阵列每个阵元宽度计算
  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了