Загрузка...

Laravel 12 | Task Scheduling | Cron Job in Laravel | Daily Database Backup | | NoviceDeveloper

📌 What is a Cron Job in Laravel?
Cron Jobs are used to automatically run tasks at scheduled times — like every minute, daily, weekly, etc.

Laravel has a built-in task scheduling system that works with the system's cron.
✅ Good part: You only need one cron entry in your server to trigger Laravel's scheduler every minute.

🛠️ Steps to Create a Cron Job in Laravel
✅ Step 1: Create Artisan Command
php artisan make:command BackupDatabase

This will create a file:
➡️ app/Console/Commands/BackupDatabase.php

Inside that file:

You’ll find a $signature variable — this is your custom command name.
Inside the handle() method, write your business logic to take a backup.

✅ Step 2: Schedule the Command
In Laravel 10 or below:

Open: app/Console/Kernel.php
Inside the schedule() method, add this line:
schedule ⮕command('db:backup')⮕ daily();

In Laravel 11 and 12:
Open: routes/console.php
Add this inside the file: Schedule::command('db:backup')daily();

✅ Step 3: Add Cron Entry in Server
Finally, you need to add one cron job on your server:
php artisan schedule:run

Видео Laravel 12 | Task Scheduling | Cron Job in Laravel | Daily Database Backup | | NoviceDeveloper канала Novice Developer
Яндекс.Метрика
Все заметки Новая заметка Страницу в заметки
Страницу в закладки Мои закладки
На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.
О CookiesНапомнить позжеПринять