Laravel 7.4 Released

Published on by

Laravel 7.4 Released image

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)
Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Paragraph

Manage your Laravel app as if it was a CMS – edit any text on any page or in any email without touching Blade or language files.

Visit Paragraph
Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $7500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
LoadForge logo

LoadForge

Easy, affordable load testing and stress tests for websites, APIs and databases.

LoadForge
Paragraph logo

Paragraph

Manage your Laravel app as if it was a CMS – edit any text on any page or in any email without touching Blade or language files.

Paragraph
Lucky Media logo

Lucky Media

Bespoke software solutions built for your business. We ♥ Laravel

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
Bacancy - Staff Augmentation logo

Bacancy - Staff Augmentation

Leave your web app development hustles to the leading IT Staff Augmentation Service Providers. Choose from an extensive pool of 1050+ developers and give yourself the sigh of success you deserve with Bacancy. Get In Touch Today!

Bacancy - Staff Augmentation
DocuWriter.ai logo

DocuWriter.ai

Save hours of manually writing Code Documentation, Comments & DocBlocks, Test suites and Refactoring.

DocuWriter.ai
Rector logo

Rector

Your partner for seamless Laravel upgrades, cutting costs, and accelerating innovation for successful companies

Rector

The latest

View all →
Add Kanban Boards to Your Laravel App in Seconds image

Add Kanban Boards to Your Laravel App in Seconds

Read article
October CMS v3.6 Ships Today, Full of New Features image

October CMS v3.6 Ships Today, Full of New Features

Read article
Laracon EU Videos are now out image

Laracon EU Videos are now out

Read article
Use Google's Gemini AI in Laravel image

Use Google's Gemini AI in Laravel

Read article
PhpStorm is getting a brand new terminal image

PhpStorm is getting a brand new terminal

Read article
Six Essential Plugins for Visual Studio Code image

Six Essential Plugins for Visual Studio Code

Read article