1

I have an API (made with Laravel) for an iOS app that is an events manager where the users have some kind of points for going to the events and if they say they will go but then they do not end up going I have to take out some points.

So my question is how can I call the function below when the event has passed to take out the points.

public function takePoints(){
    
    $postUser = post_user::where('posts_id', '=', $id);

    foreach ($postUser as $post){
        if($post->state){
            $user = User::where('id', '=', $postUser->user_id);
            
            $user->points -= 50;
            
            $user->saveOrFail();
        }
    }
}

So basically something like:

if($post->date = $todaysDate){
  $this->takePoints()
}
Juan Carlos
  • 578
  • 5
  • 22
  • Have you considered using a [PHP Cron Job](https://stackoverflow.com/questions/18737407/how-to-create-cron-job-using-php)? – abdul-wahab Jan 29 '18 at 21:58
  • `$this.takePoints()` is JS syntax, probably need to use `$this->takePoints()` to actually call the function. That aside, this sounds like a job for a CRON task. It'll run every minute, hour, day, etc and perform the check, executing the action should it pass the condition. Check https://laravel.com/docs/5.5/scheduling for more information. – Tim Lewis Jan 29 '18 at 21:58
  • 1
    yeah, my bad.I'll check it out thanks! – Juan Carlos Jan 29 '18 at 22:10

1 Answers1

2

You would use Laravel's task scheduling to run periodic checks and perform an operation when a specified time has passed.

https://laravel.com/docs/5.5/scheduling

This runs on top of cron and allows you to create commands that are run at time periods you define within the console kernel.

// add this to cron to run every minute
* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1

And from within the kernel you can schedules tasks:

$schedule->job(new Heartbeat)->everyFiveMinutes();