Blog Detail

30

Aug
What's new in Laravel 8.56 - Laravel 8.56 Released cover image

arrow_back What's new in Laravel 8.56 - Laravel 8.56 Released

Laravel is a web application framework with expressive, elegant syntax. Laravel is accessible, yet powerful, providing tools needed for large, robust applications. A superb combination of simplicity, elegance, and innovation gives you a complete toolset required to build any application with which you are tasked. Laravel has released its new version V8.56 which brings many new features and updates in the 8.x branch. Today, in this article, I will share some exciting features and updates of Laravel 8.56 with you.

Added firstOrFail to Collections

Powellblyth recreates the FirstOrFail methods from Eloquent collections into the Illuminate Support Collection and LazyCollection, which makes writing code a little more graceful.

See this example:

Old Way
$collectionFromUser = new Collection([
            ['name' => 'foo'],
            ['name' => 'foo'],
            ['name' => 'bar'],
        ]);
// Slightly obscure code and hard to scan
if ($collectionFromUser->where('name', 'fish')->count() === 0){
    $collectionFromUser->add('fish');
}

$this->doSomethingWith($collection);
New Way
 $collectionFromUser = new Collection([
            ['name' => 'foo'],
            ['name' => 'foo'],
            ['name' => 'bar'],
        ]);

try {
   $collectionFromUser->where('name', 'fish')->firstOrFail()
}
// An Exception? This must be important, I'm glad I scanned it
catch (ItemNotFoundException){
    // Ensure user has 'fish' in their collection
    $collectionFromUser->add('fish');
}
$this->doSomethingWith($collectionFromUser);

Added default rules in conditional rules

Bastien-phi introduced ConditionalRules to conditionally add rules to the validation.

public function rules()
{
    return [
        'email' => [
            Rule::when($this->someComplexTest(), ['required']),
            Rule::when(!$this->someComplexTest(), ['nullable']),
        ]
    ];
}

Now this update adds the ability to define the behavior when the condition is false.

public function rules()
{
    return [
        'email' => [
            Rule::when($this->someComplexTest(), ['required'], ['nullable']),
        ]
    ];
}

Add fullUrlWithoutQuery method to Request

Ali Saleem added an amazing feature that provides a way to add query parameters to the current URL using request()->fullUrlWithQuery() helper. This works well when you want to add filters to the current URL. But it isn’t as easy to remove a parameter.
When the current URL is https://example.com/?color=red&shape=square&size=small

request()->fullUrlWithoutQuery('color');
// https://example.com/?shape=square&size=small

request()->fullUrlWithoutQuery(['color', 'size']);
// https://example.com/?shape=square

Added Blade component slot attributes

Dan Harrin further enhances the power of Blade components by allowing slots to have their own attributes.

Here’s an example:

Old Way

@props([
    'heading',
    'footer',
])
<div {{ $attributes->class(['border']) }}>
    <h1 {{ $heading->attributes->class(['text-lg']) }}>
        {{ $heading }}
    </h1>
    {{ $slot }}
    <footer {{ $footer->attributes->class(['text-gray-700']) }}>
        {{ $footer }}
    </footer>
</div>
New Way
<x-card class="shadow-sm">
    <x-slot name="heading" class="font-bold">
        Heading
    </x-slot>
    Content
    <x-slot name="footer" class="text-sm">
        Footer
    </x-slot>
</x-card>

Now you can fully customize multiple areas of the Blade component, not just its outer container. You’ve to pass the class attribute to both slots, and you can make the heading bold and the footer smaller without having to create another view just for this use case.

Colon port support in serve host option.

Haider Ali adds support to set the port in --host option of artisan server command. This practice is pretty common in devops|sysadmins people. Please see the example below:

php artisan serve --host=localhost:8888

In case –port option is provided then it will take precedence. For example below command will use port 8080 instead of 8888.

php artisan serve --host=localhost:8888 --port=8080

There are many other changes in Laravel v8.56.0. If you want to view other changes you can visit its new updates and features on github.

Published at : 30-08-2021

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project