Laravel where with Carbon addMinutes not working

Laravel where with Carbon addMinutes not working

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Laravel where with Carbon addMinutes not working

You're trying to query events that are bookable, meaning the event time must be in the future relative to the required notice period. Your model has a time column and a notice column (stored as an integer representing minutes). The goal is to filter where time is greater than now - notice.

The issue lies in how you're passing the column value into the Carbon method. In your scope, you have:

$q->where('time', '>', Carbon::now()->addMinutes('notice'));

The problem is that 'notice' is a string literal, not the actual database column name. When Carbon's addMinutes method receives a string like 'notice', it doesn't know how to interpret it as a column reference. It treats it as a literal minute count, effectively adding 0 minutes because there's no way for Carbon to resolve that string to a dynamic value at query time.

To fix this, you need to calculate the subtraction dynamically based on the notice column from each row. However, there's a catch: Carbon methods like addMinutes don't support direct column references in their arguments. They expect integer values or specific string formats that they can parse.

The correct approach is to use Laravel's query builder to construct the comparison logic without relying on Carbon for the dynamic calculation. Instead of trying to pass the column as an argument to addMinutes, you should compare the event time against a calculated threshold using raw SQL or a closure-based where condition if needed.

Here's how you can rewrite the scope:

public function scopeBookable($query)
{
    return $query->whereRaw('time > (NOW() - INTERVAL ? MINUTE)', [DB::raw('(SELECT notice FROM events LIMIT 1)')]);
}

Wait, that approach has a flaw—it only reads the notice from one row, which won't work for filtering across all rows. You actually need to apply the condition per row. Since Carbon can't dynamically accept column values in this context, you have two solid options:

Option 1: Use Raw SQL with Dynamic Values

You can build the raw expression directly:

public function scopeBookable($query)
{
    return $query->where(function ($q) {
        $q->where('time', '>', DB::raw('DATE_SUB(NOW(), INTERVAL notice MINUTE)'));
    })->orderBy('time', 'ASC')->get();
}

This constructs a sub-expression where each row's notice value is used to calculate the threshold dynamically. This works because DATE_SUB accepts column names directly, and Laravel will substitute them correctly during query execution.

Option 2: Use Carbon with Explicit Calculation (Not Recommended for Queries)

Alternatively, if you're not using scopes and just doing a manual query, you could do:

$now = Carbon::now();
$threshold = $now->subMinutes($event['notice']); // This won't work in a WHERE clause directly

But this pattern fails in where clauses because the value needs to be dynamic per row. That's why raw SQL is the preferred solution here.

Why This Matters

The original code treated 'notice' as a string literal, causing Carbon to interpret it as zero minutes. The fix ensures that each event is evaluated against its own notice period, not a static offset. This is critical for business logic where different events may have different booking windows.

If you're building this in an API or admin panel, consider whether the notice field should be nullable or have a default value. If some events don't specify a notice period, you might want to handle those separately:

public function scopeBookable($query)
{
    return $query->where(function ($q) {
        $q->whereNotNull('notice')
          ->orWhere('time', '>', DB::raw('DATE_SUB(NOW(), INTERVAL notice MINUTE)'));
    })->orderBy('time', 'ASC')->get();
}

This adds a fallback where events without a defined notice period are considered bookable immediately. Always validate your assumptions about nullable fields before shipping.

Our network