Laravel Feature Deep Dive: WhereLike, Advanced Mail Assertions, and Microsecond Time Travel

Overview

Modern application development demands both developer velocity and code clarity. The latest updates to the

framework (v11.16 & 11.17) focus on reducing boilerplate in database queries, simplifying mail testing, and introducing high-precision time manipulation. These features allow developers to write more expressive code that mirrors natural language.

Prerequisites

To follow this guide, you should be comfortable with

syntax and have a basic understanding of the
Eloquent
ORM. Familiarity with
PEST
or
PHPUnit
for testing is recommended.

Key Libraries & Tools

  • Laravel
    : The primary PHP framework used for web development.
  • Eloquent
    : Laravel's built-in ORM for database interactions.
  • Carbon
    : The library powering Laravel's time manipulation features.

Fluent Database Queries with WhereLike

Traditionally, performing a partial match in

required passing the like operator as a string argument. While functional, it felt clunky.

// The old way
$podcasts = Podcast::where('title', 'like', '%Laravel%')->get();

// The new fluent way $podcasts = Podcast::whereLike('title', '%Laravel%')->get();


This update includes `whereNotLike`, `orWhereLike`, and `orWhereNotLike`. It abstracts the underlying SQL, ensuring consistent behavior across [MySQL](entity://software/MySQL) and [SQLite](entity://software/SQLite) without manual operator injection.

## Streamlined Mail Testing
Testing that an email reached the correct recipient used to require a closure and a manual boolean check within `Mail::assertSent`. You can now pass the recipient's email directly as the second argument.

```php
// Before: Verbose closure
Mail::assertSent(PodcastPublished::class, function ($mail) use ($user) {
    return $mail->hasTo($user->email);
});

// After: Direct assertion
Mail::assertSent(PodcastPublished::class, $user->email);

You can also pass an array of emails if you expect multiple recipients, making your test suite significantly more readable.

High-Precision Time Travel

While travel(10)->seconds() is common for testing expires_at logic, high-frequency systems like trading platforms or benchmarking tools require finer control.

now supports microsecond precision.

// Travel and freeze for precision
$this->freezeTime();
$this->travel(10)->microseconds();

$this->get('/podcast')
     ->assertSee('created 10 microseconds ago');

Tips & Gotchas

When testing microseconds, always use freezeTime(). Without it, the few milliseconds it takes for the CPU to execute the next line of code will cause your assertion to fail because the system clock continues to tick.

2 min read