DEV Community

Biozed Hossain
Biozed Hossain

Posted on

πŸš€ Automate Laravel Blog Publishing with Scheduler and Cron Jobs

Managing a blog platform can be overwhelming, especially when you need to publish posts at the right time. Luckily, Laravel provides powerful tools for automation. In this post, I’ll walk you through how I built an auto-publishing blog system using Laravel's Task Scheduler and cron jobs.

πŸ› οΈ What I Built
A feature where:

Blog posts are scheduled with a publish_date.

Posts are automatically published when their publish_date is reached.

No manual intervention is needed.

Scheduled command runs every minute using cron.

🧩 Database Setup
In the blogs table, I added two important fields:

$table->boolean('publish')->default(0); // 0 = draft, 1 = published, 2 = scheduled
$table->timestamp('publish_date')->nullable(); // schedule time
publish = 2 means the blog is scheduled.
Enter fullscreen mode Exit fullscreen mode

publish = 1 means it's visible/public.

🧠 Creating the Artisan Command
I created a custom command to auto-publish blogs:

php artisan make:command AutoPublishBlogs
Enter fullscreen mode Exit fullscreen mode
Inside App\Console\Commands\AutoPublishBlogs.php:
Enter fullscreen mode Exit fullscreen mode
public function handle()
{
    $now = \Carbon\Carbon::now();

    $blogs = \App\Models\Blog::where('publish', 2)
        ->where('publish_date', '<=', $now)
        ->update(['publish' => 1]);

    $this->info("{$blogs} blog(s) auto-published.");
    \Log::info("Auto-published {$blogs} blog(s) at " . now());

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

🧰 Registering the Command
Inside App\Console\Kernel.php:

protected $commands = [
    \App\Console\Commands\AutoPublishBlogs::class,
];
Enter fullscreen mode Exit fullscreen mode

And in the schedule() method:

protected function schedule(Schedule $schedule)
{
    $schedule->command('blogs:auto-publish')->everyMinute();
}
Enter fullscreen mode Exit fullscreen mode

πŸ•°οΈ Setting Up the Cron Job
To make Laravel's scheduler work, I set up a cron job to call Laravel’s scheduler every minute.

crontab -e
Enter fullscreen mode Exit fullscreen mode

Then I added this line (update path as per your Laravel project):

* * * * * cd /home/bio/development/topasia/topasia-new && php artisan schedule:run >> /dev/null 2>&1
Enter fullscreen mode Exit fullscreen mode

This runs Laravel’s scheduler every minute, which in turn checks for scheduled blogs to publish.

βœ… Testing It All
I tested it by:

Creating a blog post with publish = 2.

Setting the publish_date a few minutes in the future.

Watching it auto-publish as the minute changed (thanks to the scheduler!).

You can also manually trigger the scheduler with:

php artisan schedule:run
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Bonus: Showing Only Published Blogs
In various frontend queries, I updated blog fetching logic to show only published ones:

Blog::where('publish', 1)->orderBy('publish_date', 'desc')->paginate(12);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)