I have a problem where I really do not know how to make an event that listens to an action in Laravel

0

I have a slide show of images where the images registered in the bank appear. The table is called events . In this table there is a field called data_fim , which would refer to the end of the event date, and I also have a field called status , where it is of type boolean that when it is true it is active on the slide. What do I need for a service to listen, which, when the current date (date ('d')) is no longer equal to the date of the date_fim field it is disabled in the status == false field . How would you do such a feat? It was doing so see:

$evento = Evento::whereDay('data_fim', '<='  ,date('d'))->first();
    if($evento){
         $evento->status = false;
         $evento->save();
    }

More I think this is a lot of gambiarra. There is a smarter way to do the same. I would like to learn with services in Laravel. I do not know very well. I ask you to orient me.

    
asked by anonymous 24.09.2017 / 00:48

1 answer

-1

One way you can do this is to create a scheduled task using crontab

A formula similar to this:

10 0 * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1

Basically, the instruction I pass to the server, is that it runs this script, every day at 00:10.

So what you can do is create a script that checks the end_date and compares with the current date, and depending on the result, you update the status field > in your events table

Here's more in the documentation: link

And also has this site that helps when you set up the task: link

    
25.09.2017 / 20:42