Technology Blog

Look deep into latest news and innovations happening in the Tech industry with our highly informational blog.

Laravel 7.4 – The latest minor release of Laravel 7

hkis

Before we move directly to talk about 7.4 release, let’s go through each of the earlier minor releases of Laravel 7.

Laravel 7.1 – Patch to Fix Potential XSS Attacks

We will briefly look at the new features in the 7.1 release, which introduced a convenient API resource method to work with new route caching in Laravel 7.x and the ability to customize the constrained table name.

RouteRegistrar apiResource() Method

Lasse Rafn contributed an apiResource() convenience method to work with the new Laravel 7.x caching.

Since Laravel 7.X has a new (optimized) routing implemented, route names are more important, and caching routes will break if naming collisions happen.

Normally you can use Route::name(‘posts’)->resource(…… to change the name of a group (useful for nested routes like: /posts/{post}/comments).

However, this is not possible with apiResource.

We propose this change to allow that. It’s just a convenience to replace:

 
/ Before
Route::name('posts')
    ->resource(
        'posts/{post}/comments',
        'PostCommentsController'
    )
    ->only([
        'index', 'show', 'store',
        'update', 'destroy'
    ]);

// Using the apiResource() method
Route::name('posts')
    ->apiResource(
        'posts/{post}/comments',
        'PostCommentsController'
    );

Customized constrained() Table Name

Samuel França contributed the ability to pass a table name to the constrained() method in the ForeignIdColumnDefinition class:

Here’s one example from the tests:

$blueprint
    ->foreignId('team_column_id')
    ->constrained('teams');

Laravel 7.2

This version was released with HTTP client query string support and a new timeout configuration option for the SMTP mail driver. Let’s check out the new features real quick!

The expectsConfirmation Test Method

ShawnCZek contributed the expectsConfirmation() method on the PendingCommand class used to test artisan commands:

   $this->artisan('foo:bar')
    ->expectsConfirmation('Do you want to continue?', 'no')
    ->assertExitCode(1);

The confirmation assertion uses expectsQuestion under the hood, but asserts the actual value from the test. The same above would originally need to be:

$this->artisan('foo:bar')
    ->expectsConfirmation('Do you want to continue?', true)
    ->assertExitCode(1);

SMTP Mail Driver Timeout

Markus Podar contributed a timeout configuration for the SMTP mail driver. The default is 30 seconds. If you want to tweak the default, add a custom timeout configuration in seconds:

'timeout' => 60, // seconds

HTTP Client Query String Support

Irfaq Syed contributed query string support to the Laravel HTTP Client, meaning you can pass a second argument to Http::get():

Here’s an example of how it works:

Http::get('https://example.com/get');
// URL: https://example.com/get

Http::get('https://example.com/get?abc=123');
// URL: https://example.com/get?abc=123

Http::get('https://example.com/get', ['foo' => 'bar']);
// URL: https://example.com/get?foo=bar

Http::get('https://example.com/get', 'foo=bar');
// URL: https://example.com/get?foo=bar

Note that passing query params to get() overrides any present in the URI, so use one or the other.

Laravel 7.3

This version was released with the ability to use ^4.0 versions of ramsey/uuid. Since the release of Laravel 7.2, a few patch releases are available that we’ll briefly cover.

Ability to use Ramsey UUID V4

Laravel 7.3 adds the possibility to use ^4.0 of ramsey/uuid, but still supports v3.7 as well. The composer dependency is now ^3.7|^4.0.

Component Fixes

Laravel 7.2.2 fixes a few blade component issues. Notably, the make:component command now supports sub directories:

php artisan make:component Navigation/Item

# previously creates the following:
  # View/Components/Navigation/Item.php
  # views/components/item.blade.php

# Now creates them as expected:
  # View/Components/Navigation/Item.php
  # views/components/navigation/item.blade.php

Fix Route Naming Issue

Laravel 7 introduced route caching speed improvements, but with that have been a few issues with apps in-the-wild. Laravel 7.2.1 fixed a route naming issue with cache; you should upgrade to the latest 7.x release to get the newest routing fixes.

It’s important to note that you should ensure the uniqueness of route names, as routes with duplicate names can “cause unexpected behavior in multiple areas of the framework.”

Finally, Laravel 7.4

The latest version is released 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.

Custom model caster interface, a “when” higher-order collection proxy, and the ability to clear an existing “order” from the query builder are the new features of Laravel 7.4.

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');

For more information and to develop web application using Laravel, Hire Laravel Developer from us as we give you a high-quality product by utilizing all the latest tools and advanced technology. E-mail us any clock at – hello@hkinfosoft.com or Skype us: “hkinfosoft”. To develop any custom web apps using Laravel, please visit our technology page.

Content Source:

  1. laravel-news.com