Tinkerwell - The PHP Scratchpad

Laravel 7.4 Released

Published on by

Laravel 7.4 Released image

Never Miss a Laravel Release 🚀

Sign up and get an email with each new Laravel release

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
Bacancy

Outsource a dedicated Laravel developer for $3,200/month. With over a decade of experience in Laravel development, we deliver fast, high-quality, and cost-effective solutions at affordable rates.

Visit Bacancy
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
Bacancy logo

Bacancy

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

Bacancy
Tinkerwell logo

Tinkerwell

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

Tinkerwell
Cut PHP Code Review Time & Bugs into Half with CodeRabbit logo

Cut PHP Code Review Time & Bugs into Half with CodeRabbit

CodeRabbit is an AI-powered code review tool that specializes in PHP and Laravel, running PHPStan and offering automated PR analysis, security checks, and custom review features while remaining free for open-source projects.

Cut PHP Code Review Time & Bugs into Half with CodeRabbit
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
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
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

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
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant 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

The latest

View all →
"Don't Remember" Form Helper Added in Inertia.js 2.3.7 image

"Don't Remember" Form Helper Added in Inertia.js 2.3.7

Read article
Fast Laravel Course Launch image

Fast Laravel Course Launch

Read article
Laravel News Partners With Laracon India image

Laravel News Partners With Laracon India

Read article
A new beta of Laravel Wayfinder just dropped image

A new beta of Laravel Wayfinder just dropped

Read article
Ben Bjurstrom: Laravel is the best Vibecoding stack for 2026 image

Ben Bjurstrom: Laravel is the best Vibecoding stack for 2026

Read article
Laravel Altitude - Opinionated Claude Code agents and commands for TALL stack development image

Laravel Altitude - Opinionated Claude Code agents and commands for TALL stack development

Read article