The Laravel team has unveiled Laravel 10.4, packed with new features and improvements for the renowned PHP framework. In this article, we’ll provide a brief overview of the major new features.

  1. File::json() method: Laravel 10.4 introduces the File::json() method, streamlining the process of retrieving JSON encoded data from a file. This simplifies reading JSON data and decoding it.

Before:

$contents = File::get('sample.json');
$data = json_decode($contents, true);

After:

$data = File::json('sample.json');
  1. Unsupported media type assertion: A new assertion helper for the 415 Unsupported Media Type response status code has been added, enabling easier testing of unsupported media types in API responses.
$response->assertUnsupportedMediaType();
  1. Converting HasMany to HasOne relationships: Laravel 10.4 simplifies the process of defining multiple relationships in your models by enabling the conversion of HasMany relationships to HasOne relationships, and MorphMany to MorphOne.

Before:

class User extends Model
{
    public function logins(): HasMany {
        return $this->hasMany(Login::class, 'some_id', 'the_other_id');
    }
    public function latestLogin(): HasOne {
        return $this->hasOne(Login::class, 'some_id', 'the_other_id')->latestOfMany();
    }
}

After:

class User extends Model
{
    public function logins(): HasMany {
        return $this->hasMany(Login::class, 'some_id', 'the_other_id');
    }
    public function latestLogin(): HasOne {
        return $this->logins()->one()->latestOfMany();
    }
}

Macroable method for paginationInformation:

Laravel 10.4 now allows developers to define a macro for paginationInformation, making it possible to customize pagination information without the need to extend all resources as a base resource.

class ResourceCollectionMixin
{
    public function paginationInformation(): Closure
    {
        return fn ($request, $paginated, $default) => collect($default)->mapWithKeysRecursively(fn ($item, $key) => [Str::camel($key) => $item])->toArray();
    }
}

These new features, along with numerous fixes and enhancements, make Laravel 10.4 a substantial update to the framework. To learn more about the changes in Laravel 10.4, consult the changelog and the comprehensive list of new features and updates on GitHub.

LEAVE A REPLY

Please enter your comment!
Please enter your name here