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
Prerequisites
To follow this guide, you should be comfortable with
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 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.
// 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.
