I’m having a problem where I really don’t know how to do an event that hears an action on the Laravel

Asked

Viewed 66 times

0

I have a slide show of images where the images registered in the database appear. The table is called events. In this table there is a field called end date, which would refer to the end of the event date, and I also have a field called status, where he’s kind of Boolean that when she’s like true is active on the slide. What I need for a service to listen to, when the current date (date(’d)) is no longer equal to the field date end date that it be disabled the status field == false. How would you accomplish this feat? I was doing so see:

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

The more I think this is very gambiarra. There is a smarter way to do the same. I would like to learn from services in Laravel. I do not know very well. I ask you to guide me.

  • You want to always check before saving?

  • I don’t want when the event date passes the current date that it deactivates a table field

  • Right, but when?

  • Let’s assume the date of the event and day 28/09/2017 to day 30/09/2017 .

  • So I did so. I don’t know if this and gambiarra anymore works hehehe. Would you like your opinion: $expire = Event::whereDay('data_end', '<' ,date(’d'))->Where('status' , true)->Where('expire', true)->first(); // dd($expire); if(isset($expire) and $expire->Count() >= 1 ){ $expire->expire = false; $expire->save(); }

1 answer

-1

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

A formula similar to that:

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 compare with the current date, and depending on the result, you update the field status on your table events

Here are more things in the documentation: https://laravel.com/docs/5.5/scheduling

And there’s also this site that helps when you set up the task: https://crontab.guru

  • a crontab to display a slideshow? Are you sure this is the author’s problem, as he said "event" in the case refers to a table in the bank and does not necessarily seem to me real events.

  • I believe this is one of the ways to solve his problem. From what I understand, he needs to check if the current date is less than or equal to end date table. crontab is not to display or not the slideshow, but to update the field status table events.

  • crontab for a bank check looks like a round the world to get to the corner, his problem is just check the dates and allow to display the slide at that time, this is solved with the Where of the Eloquent itself or even with a if basic, using CRONTAB for this is the same as putting 200 horses to pull a car that is running, ie will give more work and in the end will do the same thing you could just starting the car.

  • I don’t think you understand the author’s question. The problem isn’t just displaying, it’s updating the field status. | ;I I believe that using crontab is rather a solution, if it will cost more resource than it needs, it depends on what the application needs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.