Laravel 7.4 Released

News

April 1st, 2020

laravel7.jpg

The Laravel team released v7.4.0 yesterday with quite a few new features, such as a custom model caster interface, a “when” higher-order collection proxy, and the ability to clear an existing “order” from the query builder:

Higher Order “when” Proxy for Collections

Loris Leiva contributed the ability to use a higher-order proxy with the Collection::when() method:

1// With this PR, this:
2$collection->when($condition, function ($collection) use ($item) {
3 $collection->push($item);
4});
5 
6// ... can be refactored as this:
7$collection->when($condition)->push($item);

This PR enables you to chain other higher-order proxy methods:

1// This:
2$collection->when($condition, function ($collection) {
3 $collection->map->parseIntoSomething();
4});
5 
6// ... can be refactored as this:
7$collection->when($condition)->map->parseIntoSomething();

Artisan expectsChoice() Assertion

Adrian Nürnberger contributed a console test method for asking choices.

Given the following example:

1$name = $this->choice('What is your name?', ['Taylor', 'Dayle'], $defaultIndex);

You could only assert the message of this question, but cannot test the given choices:

1$this->artisan('question')
2 ->expectsQuestion('What is your name?', 'Taylor')
3 ->assertExitCode(0);

As of Laravel 7.4, you can now do the following:

1$this->artisan('question')
2 ->expectsChoice('What is your name?', 'Taylor', ['Taylor', 'Dayle'])
3 ->assertExitCode(0);

You can also guarantee the order of choices by passing a fourth boolean argument:

1$this->artisan('question')
2 ->expectsChoice('What is your name?', 'Taylor', ['Taylor', 'Dayle'], true)
3 ->assertExitCode(0);

Default Values for the @props Blade Tag

@nihilsen contributed the ability to define default props via @props:

1<!-- Previously you might need something like: -->
2@props(['type', 'message'])
3@php
4 $type = $type ?? 'info'
5@endphp
6 
7<!-- New defaults in Laravel >=7.4 -->
8@props(['type' => 'info', 'message'])

Castable Interface

Brent Roose contributed a Castable interface which allows “castable” types to specify their caster classes:

1// Instead of this
2class ModelX extends Model
3{
4 protected $casts = [
5 'data' => CastToDTO::class . ':' . MyDTO::class,
6 ];
7}
8 
9 
10// You could do this
11class ModelY extends Model
12{
13 protected $casts = [
14 'data' => MyDTO::class,
15 ];
16}
17 
18// The underlying class
19use Illuminate\Contracts\Database\Eloquent\Castable;
20 
21class MyDTO implements Castable
22{
23 public static function castUsing()
24 {
25 return CastToDTO::class . ':' . static::class;
26 }
27}

Remove Orders from the Query Builder

Jonathan Reinink contributed a reorder() method to the query builder to reset orderBy() calls:

1$query = DB::table('users')->orderBy('name');
2 
3$unorderedUsers = $query->reorder()->get();

Reorder allows you to define default order in Eloquent relationships, with the ability to back out if needed:

1class Account extends Model
2{
3 public function users()
4 {
5 return $this->hasMany(User::class)->orderBy('name');
6 }
7}
8 
9// Remove the name orderBy and order by email
10$account->users()->reorder()->orderBy('email');
11 
12// The same can be written as:
13$account->users()->reorder('email');

Release Notes

You can see the full list of new features and updates below and the diff between 7.3.0 and 7.4.0 on GitHub. The full release notes for Laravel 7.x are available in the latest v7 changelog:

v7.4.0

Added

  • Makes the stubs used for make:policy customizable (#32040, 9d36a36)
  • Implement HigherOrderWhenProxy for Collections (#32148)
  • Added Illuminate\Testing\PendingCommand::expectsChoice() (#32139)
  • Added support for default values for the “props” blade tag (#32177)
  • Added Castable interface (#32129, 9cbf908, 651371a)
  • Added the ability to remove orders from the query builder (#32186)

Fixed

  • Added missing return in the PendingMailFake::sendNow() and PendingMailFake::send() (#32093)
  • Fixed custom Model attributes casts (#32118)
  • Fixed route group prefixing (#32135, 870efef)
  • Fixed component class view reference (#32132)

Changed

  • Remove Swift Mailer bindings (#32165)
  • Publish console stub when running stub:publish command (#32096)
  • Publish rule stub when running make:rule command (#32097)
  • Adding the middleware.stub to the files that will be published when running php artisan stub:publish (#32099)
  • Adding the factory.stub to the files that will be published when running php artisan stub:publish (#32100)
  • Adding the seeder.stub to the files that will be published when running php artisan stub:publish (#32122)

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.

Laravel News Partners