查询生成器多列递增

时间:2019-12-05 10:00:17

标签: php laravel-5 laravel-query-builder

我想通过增加用户帐户的All_balance,Available_balance,Credit Laravel 5.8查询生成器来更新详细信息

处理了一列

    $account = DB::table('accounts')
                            ->where(['user_id'=>$userId])
                            ->increment('all_balance',$amount);

我在下面尝试了此方法,但没有用

     $account = DB::table('accounts')
                            ->where(['user_id'=>$userId])
                            ->increment(function($increment){
                                $increment->('all_balance',$amount);
                                $increment->('available_balance',$amount);
                                $increment->('credit',$amount);
                            });

2 个答案:

答案 0 :(得分:0)

尝试一下...

@Getter
@Builder(builderClassName = "Builder", buildMethodName = "build")
public final class BusinessEvent {
    private String action, duration, outcome, provider, trackId, sequence;
    private Optional<String> brand, vin, externalTrackId, code = Optional.empty();
    private Optional<Object> eventError = Optional.empty();

    static class Builder {
        BusinessEvent build() {
            if (brand == null) {
                throw new RuntimeException("brand not set");
            }
            // Custom validation
            return new BusinessEvent(action, duration, outcome, provider, trackId, sequence, brand, vin, externalTrackId, code, eventError);
        }
    }

    public static void main(String[] args) {
        BusinessEvent eventWithBrand = BusinessEvent.builder().brand(Optional.of("brand")).build();
        // will throw exception
        BusinessEvent event = BusinessEvent.builder().build();
    }
}

答案 1 :(得分:0)

@Zar NI Ko Ko我感谢您的答复,因为它确实确实为我提供了帮助,但是由于列名和变量名($ amount)之间没有连接,它给了我错误,所以它看到了另一列。现在一切都很好

工作代码

    $account = DB::table('accounts')
            ->where(['user_id'=>$userId])
            ->update([
                'all_balance' => DB::raw('all_balance + '.$amount.''),
                'available_balance' =>DB::raw('available_balance + '.$amount.''),
                'credit' => DB::raw('credit + '.$amount.''),
              ]);