Laravel 9.49 Released

Published on by

Laravel 9.49 Released image

The Laravel team released 9.49 this week with support for casting an array of enums, CLI prompts, and more. Be sure the check out the changelog as this week's release is chock full of new additions, fixes, and changes from the last two weeks; most of the Laravel team was at Laracon EU last week.

Support for casting collection or array of enums

Ralph J. Smit contributed support for casting arrays of enums:

use App\Enums\ServerStatus;
use Illuminate\Database\Eloquent\Casts\AsEnumCollection;
 
protected $casts = [
'statuses' => AsEnumCollection::class.':'.ServerStatus::class,
];

And here's an example of using the array version:

use App\Enums\ServerStatus;
use Illuminate\Database\Eloquent\Casts\AsEnumArrayObject;
 
protected $casts = [
'statuses' => AsEnumArrayObject::class.':'.ServerStatus::class,
];

See the Casting Arrays Of Enums documentation for more information.

CLI prompts

Jess Archer contributed the ability to automatically prompt the user for missing command arguments instead of returning an error. This feature can be used by implementing the PromptsForMissingInput interface:

use Illuminate\Console\Command;
use Illuminate\Contracts\Console\PromptsForMissingInput;
 
class MyCommand extends Command implements PromptsForMissingInput
{
// ...
}

This functionality was also added to all the make:* commands:

Before:

After:

New TestResponse JSON assertions

Seth Phat contributed two assertion methods for asserting that JSON is an array and asserting that JSON is an object. Here are some examples from Pull Request #45731:

$this->json('GET', 'countries')
->assertOk()
->assertJsonIsArray(); // [ {..}, {...}, ....]
 
$this->json('GET', 'users')
->assertOk()
->assertJsonIsArray('data'); // {'data': [...]}
 
$this->json('GET', 'countries/US')
->assertOk()
->assertJsonIsObject(); // {id: '...', name: '..'}
 
$this->json('GET', 'users/1')
->assertOk()
->assertJsonIsObject('data'); // {'data': {id: '..', name: '...'}}

"missing" validation rules

Tim MacDonald contributed "missing" validation rules, which is a strict version of the prohibits validation rule. The field under validation must not be present in the input data.

Here's a list of all the possible variations, including the typical if and unless variants:

  • missing
  • missing_if:attribute,value
  • missing_unless:attribute,value
  • missing_with:attribute1,attribute2
  • missing_with_all:attribute1,attribute2

HTTP client error handling methods

Wendell Adriel contributed new HTTP response methods for error handling:

Here are a few examples of the methods Wendell contributed:

// Only throws an exception if the HTTP response code is 500
$response->throwIfStatus(500);
 
// Callable
$response->throwIfStatus(fn ($status) => $status === 500);
 
// Throws an exception if the HTTP response code is not 200
$response->throwUnlessStatus(200);
 
// Only throws an exception if the HTTP response code is >= 400 and < 500
$response->throwIfClientError();
 
// Only throws an exception if the HTTP response code is >= 500
$response->throwIfServerError();

Check out the HTTP Client error handling documentation for more details.

Configurable timezone support for queue worker output

Matias Mäki contributed a queue.log_timezone configuration option so that queue:work can output timestamps in a different timezone than the application default app.timezone setting.:

The rationale for this addition is that this is already doable for log files made through Log facade with Log::setTimezone, but the WorkCommand is not using the Logging subsystem but Console\OutputStyle to write directly back to the stdout and stderr.

No action on the deletion of foreign keys

Erfan Hemmati contributed a noActionOnDelete() method to migrations for foreign keys.

$table
->foreign('user_id')
->references('id')
->on('users')
->noActionOnDelete();

Refer to the documentation of your database of choice to understand how it might work.

Add force delete quietly to soft deleted models

Pascal Huberts contributed a forceDeleteQuietly() method that forces a hard delete on a soft-deleted model without raising any events:

$model->forceDeleteQuietly();

Array sortDesc() method

Timur Fralik contributed a Arr::sortDesc() method which sorts an array in descending order by its values:

$sorted = Arr::sortDesc(['Desk', 'Table', 'Chair']);
 
// ['Table', 'Desk', 'Chair']

Release Notes

You can see the complete list of new features and updates below and the diff between 9.48.0 and 9.49.0 on GitHub. The following release notes are directly from the changelog:

v9.49.0

Added

  • Added Illuminate/Database/Schema/ForeignKeyDefinition::noActionOnDelete() (#45712)
  • Added new throw helper methods to the HTTP Client (#45704)
  • Added configurable timezone support for WorkCommand output timestamps (#45722)
  • Added support for casting arrays containing enums (#45621)
  • Added "missing" validation rules (#45717)
  • Added /Illuminate/Database/Eloquent/SoftDeletes::forceDeleteQuietly() (#45737)
  • Added Illuminate/Collections/Arr::sortDesc() (#45761)
  • Added CLI Prompts (#45629, #45864)
  • Adds assertJsonIsArray and assertJsonIsObject for TestResponse (#45731)
  • Added Illuminate/Database/Eloquent/Relations/HasOneOrMany::createQuietly() (#45783)
  • Add validation rules: ascii_alpha, ascii_alpha_num, ascii_alpha_dash (#45769)
  • Extract status methods to traits (#45789)
  • Add "addRestoreOrCreate" extension to SoftDeletingScope (#45754)
  • Added connection established event (f850d99)
  • Add forceDeleting event to models (#45836)
  • Add title tag in mail template (#45859)
  • Added new methods to Collection (#45839)
  • Add skip cancelled middleware (#45869)

Fixed

  • Fix flushdb on cluster for PredisClusterConnection.php (#45544)
  • Fix blade tag issue with nested calls (#45764)
  • Fix infinite loop in blade compiler (#45780)
  • Fix ValidationValidator not to accept terminating newline (#45790)
  • Fix stubs publish command generating incorrect controller stubs (#45812)
  • fix: normalize route pipeline exception (#45817)
  • Fix Illuminate Filesystem replace() leaves file executable (#45856)

Changed

  • Ensures channel name matches from start of string (#45692)
  • Replace raw invisible characters in regex expressions with counterpart Unicode regex notations (#45680)
  • Optimize destroy method (#45709)
  • Unify prohibits behavior around prohibits_if (#45723)
  • Removes dependency on bcmath (#45729)
  • Allow brick/math 0.11 also (#45762)
  • Optimize findMany of BelongsToMany (#45745)
  • Ensure decimal rule handles large values (#45693)
  • Backed enum support for @js (#45862)
  • Restart syscalls for SIGALRM when worker times out a job (#45871)
  • Ensure subsiquent calls to Mailable->to() overwrite previous entries (#45885)
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
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
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
DocuWriter.ai logo

DocuWriter.ai

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

DocuWriter.ai
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
All Green logo

All Green

All Green is a SaaS test runner that can execute your whole Laravel test suite in mere seconds so that you don't get blocked – you get feedback almost instantly and you can deploy to production very quickly.

All Green
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 →
Automatic Blade Formatting on Save in PhpStorm image

Automatic Blade Formatting on Save in PhpStorm

Read article
PhpStorm 2024.1 Is Released With a Integrated Terminal, Local AI Code Completion, and More image

PhpStorm 2024.1 Is Released With a Integrated Terminal, Local AI Code Completion, and More

Read article
Laravel Prompts Adds a Multi-line Textarea Input, Laravel 11.3 Released image

Laravel Prompts Adds a Multi-line Textarea Input, Laravel 11.3 Released

Read article
Bartender Is an Opinionated Way to Authenticate Users Using Laravel Socialite image

Bartender Is an Opinionated Way to Authenticate Users Using Laravel Socialite

Read article
Jeffrey Way's PhpStorm Setup in 2024 image

Jeffrey Way's PhpStorm Setup in 2024

Read article
Easily Optimize PDFs in Laravel with the Optimizer Package image

Easily Optimize PDFs in Laravel with the Optimizer Package

Read article