Expect the following field inside a database:
$table->unsignedBigInteger('experience_points')->nullable(false)->default(0);
To increment / decrement the value the following functions are used:
$user->increment('experience_points', +50); //+50 can be $someExperienceAmountToBeIncremented
$user->increment('experience_points', -50); //-50 can be $someExperienceAmountToBeIncremented
Let's say a user triggers after creating a his user account the -50 increment function, this will throw an exception inside laravel:
Illuminate\Database\QueryException SQLSTATE[22003]: Numeric value out of range: 1690 BIGINT UNSIGNED value is out of range in '`database`.`users`.`experience_points`
is there any chance to still use increment() or am I required to perform the check every time before like:
if($user->experience_points - $someExperienceAmountToBeIncremented) < 0
$user->update(['experience_points', 0]);
Somehow using this "check" before feels bad to use, but is there any other way to handle this maybe a bit more elegant?
Because of observers "watching" on the experience_points it's not possible for me to switch to rawSQL.