Home > Back-end >  Laravel eloquent increment calculated percentage on update
Laravel eloquent increment calculated percentage on update

Time:10-14

I have an integer column, and need to increment value on a selected rows, with his percentage.

I'm trying to do some like this:

    $value = $request->percentage / 100;
    $lineaCalendario  = Calendar::where('rate_id', $idRate)
                                        ->whereIn('date', $dates) 
                                        ->increment('price', DB::raw('price')*$value); 

This don't work.

Unsupported operand types: float * Illuminate\Database\Query\Expression

But I can't find how to do it..

CodePudding user response:

You can try to use update method instead of increment and do your multiplication of the price * $value in the DB::raw() query directly. Indeed, you can't multiple your value by an expression:

Calendar::query()
    ->where('rate_id', $idRate)
    ->whereIn('date', $dates)
    ->update([
        'price' => DB::raw("price   (price * ${value})")
    ])

Notice: As described in the official documentation of Laravel:

Raw statements will be injected into the query as strings, 
so you should be extremely careful to avoid creating SQL injection vulnerabilities.
  • Related