Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vmunich's avatar

What's the proper way to handle scheduled blog posts?

Laravel's Queue::later() should only be used with short amounts of time in my opinion, something like minutes or a few hours.

But when a user of my app wants to schedule something for next month, or even next year, how should I handle it?

I was thinking of making a schedules table and add the scheduled posts there, and have a cronjob running every minute. But what should my cronjob actually do?

Also, would this be the best way of handling this, or there's something in Laravel for this kind of task that I'm not aware of?

Thanks

Edit: I forgot to mention, when a blog post goes like, I have to trigger some commands like posting to Twitter and Facebook, or send an email to the user to let them know that the post has been posted.

0 likes
5 replies
ohffs's avatar

Just have something like a 'golive' datetime field on the table and depending how your code works either use that as a filter on the returned results or a scheduled task that runs every so often and checks the date and sets a 'is_live' flag? Whatever works in your situation really :-)

vmunich's avatar

@ohffs Thanks for the input. I just edited my post, in my case just a filter won't work because I need to trigger some commands when the blog post is posted (post to Facebook and Twitter for example). Your second suggestion sounds like a start though, I will research about that!

ohffs's avatar

@vmunich no problem - I'm dealing with a similar thing just now in kind of the opposite form - gradually warning/disabling people who haven't logged in for > $some_time :-)

uxweb's avatar

@vmunich I think your are going in good direction, i will do a job that checks for posts which publish_date is now and fire an event of PostWasPublished, then a Handler will do the rest, like post it to twitter, Facebook or send an email.

That job will be scheduled to run maybe everyday by the morning.

michaeldyrynda's avatar

What I do for my blog to allow future-published posts is create a published_at datetime field. This will default to the current date and time when I hit publish. To then list my published posts, I run a query along the lines of:

$posts = App\Post::whereNotNull('published_at')
    ->where('published_at', '<', new \DateTime())
    ->latest('published_at')
    ->get();

That's what this all hinges on, really. Adding your specific logic into a scheduled command is simple. Add a flag in your posts table to take into account your extra publish steps:

$posts = App\Post::whereNotNull('published_at')
    ->where('published_at', '<', new \DateTime())
    ->where('publish_tasks_complete', true)
    ->latest('published_at')
    ->get();

You can use this same query in your command, changing the check for where('publish_tasks_complete', true) to where('publish_tasks_complete', false).

For each of these records, publish your Twitter and/or Facebook posts, then update the database flag and your post listing will update of its own accord.

2 likes

Please or to participate in this conversation.