Laravel v7.10 updates

Published on May 5th, 2020

With all the various sources for news about Laravel I still think there is more room for a small newsletter to inform about the weekly occurring changes in the Laravel ecosystem. Without further words let's dive right in!

Make Custom Casts

With Laravel 7 we got the custom Casts feature. In v7.10 Voidgraphics got us the artisan make:cast command. For those of you who are interested in how this feature was implemented, take a look at the changed files and see how easy Laravel makes it to create a generator command.

PR: https://github.com/laravel/framework/pull/32594

SMTP Auth Mode

Thanks to Fragkp we can set the auth_mode setting of the SMTP mail driver within our mail.php configs.

PR: https://github.com/laravel/laravel/pull/5293

hasNamedScope Method

With the new hasNamedScope() method we are now able to determine if a model has a given scope dynamically. This can be used to apply filters to a DB query of a simple request. Thanks Alex!

use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;

public function index(Request $request, Model $model): 
{
    $query = $model->newQuery();

    if ($model->hasNamedScope('owner')) {
        $query->owner(auth()->user());
    }

    if ($model->hasNamedScope('member')) {
        $query->member(auth()->user());
    }

    return $query->paginate();
}

PR: https://github.com/laravel/framework/pull/32622, https://github.com/laravel/framework/pull/32631

GroupBy and Having pagination and count improvements

I didn't know it before but after reading the PR opened by Taylor and the linked issues it seems like there where a lot of issues related to not working pagination and wrong count results when someone used groupBy() or having() in their queries. With this addition by Taylor the pagination counts are now run as Subquery for groupBy and havings statements.

PR: https://github.com/laravel/framework/pull/32624

Cache::lock() support for database cache driver

In v7.10 Cache::lock() also works with the database driver. We only need to add an additional cache_locks table.

// Schema
Schema::create('cache_locks', function ($table) {
    $table->string('key')->primary();
    $table->string('owner');
    $table->integer('expiration');
});

// ustilizing locks
use Illuminate\Support\Facades\Cache;

$lock = Cache::lock('foo', 10);

if ($lock->get()) {
    // Lock acquired for 10 seconds...

    $lock->release();
}

For more information about Cache locks visit the documentation.

PR: https://github.com/laravel/framework/pull/32639

New skipUntil and skipWhile Collection methods

Joseph added takeWhile() and takeUntil() in v7.9 and now we also get their counterparts skipWhile() and skipUntil().

// skipWhile
$collection = collect([1, 2, 3, 4]);

$subset = $collection->skipWhile(function ($item) {
    return $item <= 3;
});

$subset->all();

// [4]

// skipUntil
$collection = collect([1, 2, 3, 4]);

$subset = $collection->skipUntil(function ($item) {
    return $item >= 3;
});

$subset->all();

// [3, 4]

// or
$collection = collect([1, 2, 3, 4]);

$subset = $collection->skipUntil(3);

$subset->all();

// [3, 4]

PR: https://github.com/laravel/framework/pull/32672

New AssertDatabaseCount assertion

Christoph added a new assertion which makes it even more explicit when you test how many records a specific table should have.

// < v7.10
$this->assertCount(5, User::all());
// or
$this->assertTrue(5, User::count());

// >= v7.10
$this->assertDatabaseCount('users', 5);

Behind the scenes the Illuminate\Database\Query\Builder::count() method is used which is also used when calling User::count(). With the new assertion method we got a nice assertion that read out loud sounds like a part of a documentation.

$this->assertDatabaseCount('users', 5) reads as Assert that the database table 'users' contains 5 records.

PR: https://github.com/laravel/framework/pull/32597

Truth-test assertions using closures

We all know and love how easy Laravel Facades can be tested by calling the ::fake() method. But as we all know: coders are lazy and always look for ways to type less :). In v7.10 SjorsO added a new which uses some PHP-Reflection magic behind the scenes to support Closures as first argument for Facade fake assertions.

use App\Jobs\ShipOrder;

Queue::fake();

// < v7.10
Queue::assertPushed(ShipOrder::class, function ($job) use (ShipOrder $order) {
    return $job->order->id === $order->id;
});

// >= v7.10
Queue::assertPushed(function (ShipOrder $job) use ($order) {
    return $job->order->id === $order->id;
});

All other build in Laravel Facades with a fake() method like Bus, Event, Mail, Notification now support passing Closures to all their assertX() methods as the first argument.

PR: https://github.com/laravel/framework/pull/32626


Thanks for making it so far! I appreciate you taking the time to read through the article. To provide you with higher quality articles I need your feedback on how to write better articles. Did you enjoy the read? Do you have tips on what I could make better? Please tag me on twitter @krishankoenig or write me a mail to krishan.koenig@gmail.com.

Sign up for my newsletter

If you enjoyed this article, sign up for my newsletter to be the first to know when a new article is published.