How to Update User Wallet Points

Hello Friends please help me to solve this problem…

Tables : users, wallet

users: id, name, points
wallet: id, name, wlpoints, uid

Page Events Code: AfterAdd --:

$pupdate = $records[‘points’];
DB::table(users)->WHARE(‘id’, $records[‘uid’])->update([‘wlpoints’, $pupdate]);

After Submitting Wallet Points it’s replaces old value with new value… But i need add old value with current value…

Ex: old wlpoints = 25, New Wallet Points= 5;

In my case ‘25’ replacing with ‘5’… :pensive:

I need newpoints to be 30 (25+5)

you need to query your table to get the old point, then add the old point to the new point before updating the point.

Thank you for your support…
Will this work

Tables : users, wallet

users: id, name, points
wallet: id, name, wlpoints, uid

Page Events Code: AfterAdd --:

$pupdate = $records[‘points’];

$userwlpoints = DB::table(users)->WHERE(‘id’, $records[‘uid’])->get([‘wlpoints’,$records[‘uid’]]);

$totalpoints = $userwlpoints + $pupdate;

DB::table(users)->WHARE(‘id’, $records[‘uid’])->update([‘wlpoints’, $totalpoints]);

The below code should work.

$pupdate = $records['points'];
$userwlpoints = DB::table('users')->where('id', $records['uid'])->pluck('wlpoints');
$totalpoints = $userwlpoints + $pupdate;
DB::table('users')->where('id', $records['uid'])->update(['wlpoints' => $totalpoints]);
1 Like

This returns error like this

ErrorException

Object of class Illuminate\Support\Collection could not be converted to number

http://localhost:8050/wallet/store

This is my Code

    /**
     * After new record created
     * @param array $record // newly created record
     */
    private function afterAdd($record){
        //enter statement here
        
        $pupdate      = $record['points'];
        $userwlpoints = DB::table('users')->where('id', $record['uid'])->pluck('wlpoints');
        $totalpoints  = $userwlpoints + $pupdate;
        DB::table('users')->where('id', $record['uid'])->update(['wlpoints' => $totalpoints]);
    }
    

Use this instead.

$pupdate = $records['points'];
$userwlpoints = DB::table('users')->where('id', $records['uid'])->value('wlpoints');
$totalpoints = $userwlpoints + $pupdate;
DB::table('users')->where('id', $records['uid'])->update(['wlpoints' => $totalpoints]);

Please note that most of these functions are laravel functions and they are available in the laravel docs here Database: Query Builder - Laravel - The PHP Framework For Web Artisans.