Skip to main content

Overview

The reservations:check command is an automated task that processes expired reservations and updates the status of associated tables. This command runs hourly to ensure that tables are automatically marked as available once their reservation time has passed.

Command Signature

php artisan reservations:check
Description: Revisa las reservaciones y actualiza el estado de las mesas (Reviews reservations and updates table status)

What It Does

The command performs the following operations:
1

Find Expired Reservations

Queries all reservations where both the date and time are less than or equal to the current date/time.
2

Update Table Status

For each expired reservation, updates the associated table’s status to disponible (available).
3

Delete Reservations

Removes the expired reservation records from the database.
4

Log Result

Outputs a success message indicating that expired reservations have been processed.

Implementation

Full Command Class

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\Table;
use App\Models\Reservation;
use Carbon\Carbon;

class CheckReservations extends Command
{
    protected $signature = 'reservations:check';
    protected $description = 'Revisa las reservaciones y actualiza el estado de las mesas';

    public function handle()
    {
        $now = Carbon::now();

        // Encuentra las reservaciones pasadas
        $expiredReservations = Reservation::where('date', '<=', $now->toDateString())
            ->where('time', '<=', $now->toTimeString())
            ->get();

        foreach ($expiredReservations as $reservation) {
            // Actualiza la mesa a disponible
            $table = $reservation->table;
            if ($table) {
                $table->update(['status' => 'disponible']);
            }

            // Elimina la reservación
            $reservation->delete();
        }

        $this->info('Reservaciones vencidas procesadas correctamente.');
    }
}

How It Works

  1. Current Time Reference: Uses Carbon::now() to get the current date and time
  2. Query Expired Reservations: Finds reservations where:
    • date is less than or equal to today’s date
    • time is less than or equal to the current time
  3. Process Each Reservation:
    • Retrieves the associated Table model via the relationship
    • Updates the table status to disponible
    • Deletes the reservation record
  4. Output: Displays success message
This command permanently deletes expired reservation records. Ensure you have proper logging or backups if you need to maintain historical reservation data.

Manual Execution

You can run this command manually at any time:
php artisan reservations:check
Expected Output:
Reservaciones vencidas procesadas correctamente.

Scheduled Execution

Cron Schedule Configuration

The command is configured to run automatically every hour in app/Console/Kernel.php:
protected function schedule(Schedule $schedule): void
{
    $schedule->command('reservations:check')->hourly();
}

Setting Up Laravel Scheduler

To enable automatic execution, you need to add a single cron entry to your server:
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
This cron entry runs Laravel’s scheduler every minute, which then executes the reservations:check command hourly according to the schedule defined in the Kernel.
1

Edit Crontab

Open your crontab editor:
crontab -e
2

Add Scheduler Entry

Add the Laravel scheduler cron entry:
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
3

Save and Verify

Save the crontab and verify it’s active:
crontab -l
Make sure to replace /path-to-your-project with the actual absolute path to your Laravel application.

Output and Logging

Console Output

When executed, the command outputs:
  • Success message: "Reservaciones vencidas procesadas correctamente."

Logging Recommendations

While the command doesn’t include built-in logging, you can monitor its execution: View Scheduled Tasks:
php artisan schedule:list
Test Schedule Without Waiting:
php artisan schedule:work
Add Custom Logging: You can extend the command to include logging:
use Illuminate\Support\Facades\Log;

public function handle()
{
    $now = Carbon::now();
    $expiredReservations = Reservation::where('date', '<=', $now->toDateString())
        ->where('time', '<=', $now->toTimeString())
        ->get();

    $count = $expiredReservations->count();
    
    foreach ($expiredReservations as $reservation) {
        $table = $reservation->table;
        if ($table) {
            $table->update(['status' => 'disponible']);
        }
        $reservation->delete();
    }

    Log::info("Processed {$count} expired reservations");
    $this->info('Reservaciones vencidas procesadas correctamente.');
}

Use Cases

  1. Automatic Table Turnover: Ensures tables become available automatically after reservation times pass
  2. Database Maintenance: Keeps the reservations table clean by removing old records
  3. Real-time Availability: Maintains accurate table availability status for new reservations
  4. Staff Efficiency: Reduces manual intervention needed to update table statuses

Troubleshooting

Command Not Running Automatically

1

Check Cron Entry

Verify the cron entry is correct:
crontab -l
2

Test Schedule

Run the scheduler manually to test:
php artisan schedule:run
3

Check Logs

Review Laravel logs for errors:
tail -f storage/logs/laravel.log

No Reservations Being Processed

  • Verify there are expired reservations in the database
  • Check the system time is correct
  • Ensure the Reservation model has the correct table name and relationships
The command uses Carbon::now() which respects your application’s timezone setting in config/app.php. Ensure your timezone is configured correctly.

Build docs developers (and LLMs) love