Reputation: 1
I have a wallet charge in my project,now I want when user add another money ,insert this to Previous value,how should I do that ? could I push it in a variable then add them?
public function get_data_wallet(Request $request)
{
$request->validate([
'final_price' => 'required',
]);
$res =Auth()->user()->update([
'wallet' => floatval($request->final_price),
]);
return show_message($res, 'success');
}
Upvotes: 0
Views: 131
Reputation: 18926
Fetch the user object, get the wallet value, add the new value to the wallet value and save that to the wallet property. If wallet is not set default to 0.
$user = Auth()->user();
$user->wallet = ($user->wallet ?? 0) + floatval($request->final_price);
$user->save();
For securing the wallet
property is a float
, i would cast it on the model.
protected $casts = [
'wallet' => 'float',
];
Upvotes: 1