KingsleyO
2 months ago

How to create a new record if not exist or update if it exists

Posted 2 months ago by KingsleyO

I Have this code that increments an already existing record

$credit = Credit::where('user_id', Auth::getUser()->id)->first();
 $credit->increment('amount', 600);
 $credit->fresh()->amount;

If the record does not already exist it throws up this error

"Call to a member function increment() on null"

How do i create the record if it does not already exist but if it exists, update it

Please sign in or create an account to participate in this conversation.