i am running a scheduler when user point reach 5000p i want to automatically deduct the point , i am using this package to handle user points https://github.com/ansezz/laravel-gamify the point deduction isnt working kindly assist
public function handle()
{
$users = User::with('points')->get();
foreach ($users as $user) {
if ($user->achieved_points >= 5000) {
$user->user->achieved_points - $user->achieved_points;
}
}
}
CodePudding user response:
My guess is that you should use the package specific method : undoPoint().
You can try something like :
public function handle()
{
$users = User::has('points', '>=', 5000)->get();
foreach ($users as $user) {
foreach ($user->points as $point) {
$user->undoPoint($point);
}
}
}
Note that I've also update your query to only include user with more than 5000p, see Querying Relathionship Existence.
CodePudding user response:
public function handle()
{
$users = User::with('points')->get();
foreach ($users as $user) {
if ($user->achieved_points >= 5000) {
$user->user->achieved_points->decrement($user->achieved_points);
}
}
}