Laravel 7.4 Released

Laravel 7.4 Released

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:

// With this PR, this:
$collection->when($condition, function ($collection) use ($item) {
    $collection->push($item);
});

// ... can be refactored as this:
$collection->when($condition)->push($item);

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

// This:
$collection->when($condition, function ($collection) {
    $collection->map->parseIntoSomething();
});

// ... can be refactored as this:
$collection->when($condition)->map->parseIntoSomething();

Artisan expectsChoice() Assertion

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

Given the following example:

$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:

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

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

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

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

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

Default Values for the @props Blade Tag

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

<!-- Previously you might need something like: -->
@props(['type', 'message'])
@php
    $type = $type ?? 'info'
@endphp

<!-- New defaults in Laravel >=7.4 -->
@props(['type' => 'info', 'message'])

Castable Interface

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

// Instead of this
class ModelX extends Model
{
    protected $casts = [
        'data' => CastToDTO::class . ':' . MyDTO::class,
    ];
}

// You could do this
class ModelY extends Model
{
    protected $casts = [
        'data' => MyDTO::class,
    ];
}

// The underlying class
use Illuminate\Contracts\Database\Eloquent\Castable;

class MyDTO implements Castable
{
    public static function castUsing()
    {
        return CastToDTO::class . ':' . static::class;
    }
}

Remove Orders from the Query Builder

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

$query = DB::table('users')->orderBy('name');

$unorderedUsers = $query->reorder()->get();

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

class Account extends Model
{
    public function users()
    {
        return $this->hasMany(User::class)->orderBy('name');
    }
}

// Remove the name orderBy and order by email
$account->users()->reorder()->orderBy('email');

// The same can be written as:
$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: News / Laravel 7 / Releases

Newsletter

Join 31,000+ others and never miss out on new tips, tutorials, and more.

Laravel News Partners

Laravel Jobs

Senior Laravel Developer
Remote
EC Pro
Senior Full-Stack Engineer
[REMOTE: USA Only] All Other Applications Will be Rejected Immediately"
Curricula
Senior Full Stack PHP Developer
Canada
Motto Design Studio
Software Engineer
Remote, USA Only
Grow Progress
Lead Backend PHP Developer
Hamburg, Deutschland
ABOUT YOU GmbH