Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Laravel eloquent increment calculated percentage on update

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

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

>Solution :

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.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading