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
No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project.

Visit No Compromises
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
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
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
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
Larafast: Laravel SaaS Starter Kit logo

Larafast: Laravel SaaS Starter Kit

Larafast is a Laravel SaaS Starter Kit with ready-to-go features for Payments, Auth, Admin, Blog, SEO, and beautiful themes. Available with VILT and TALL stacks.

Larafast: Laravel SaaS Starter Kit
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Rector logo

Rector

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

Rector

The latest

View all →
Anonymous Event Broadcasting in Laravel 11.5 image

Anonymous Event Broadcasting in Laravel 11.5

Read article
Microsoft Clarity Integration for Laravel image

Microsoft Clarity Integration for Laravel

Read article
Apply Dynamic Filters to Eloquent Models with the Filterable Package image

Apply Dynamic Filters to Eloquent Models with the Filterable Package

Read article
Property Hooks Get Closer to Becoming a Reality in PHP 8.4 image

Property Hooks Get Closer to Becoming a Reality in PHP 8.4

Read article
Asserting Exceptions in Laravel Tests image

Asserting Exceptions in Laravel Tests

Read article
Reversible Form Prompts and a New Exceptions Facade in Laravel 11.4 image

Reversible Form Prompts and a New Exceptions Facade in Laravel 11.4

Read article