The Laravel group has unveiled Laravel 10.4, filled with new options and enhancements for the famend PHP framework. On this article, we’ll present a short overview of the main new options.
- File::json() methodology: Laravel 10.4 introduces the File::json() methodology, streamlining the method of retrieving JSON encoded information from a file. This simplifies studying JSON information and decoding it.
Earlier than:
$contents = File::get('pattern.json');
$information = json_decode($contents, true);
After:
$information = File::json('pattern.json');
- Unsupported media kind assertion: A brand new assertion helper for the 415 Unsupported Media Kind response standing code has been added, enabling simpler testing of unsupported media varieties in API responses.
$response->assertUnsupportedMediaType();
- Changing HasMany to HasOne relationships: Laravel 10.4 simplifies the method of defining a number of relationships in your fashions by enabling the conversion of HasMany relationships to HasOne relationships, and MorphMany to MorphOne.
Earlier than:
class Consumer extends Mannequin
{
public operate logins(): HasMany {
return $this->hasMany(Login::class, 'some_id', 'the_other_id');
}
public operate latestLogin(): HasOne {
return $this->hasOne(Login::class, 'some_id', 'the_other_id')->latestOfMany();
}
}
After:
class Consumer extends Mannequin
{
public operate logins(): HasMany {
return $this->hasMany(Login::class, 'some_id', 'the_other_id');
}
public operate latestLogin(): HasOne {
return $this->logins()->one()->latestOfMany();
}
}
Macroable methodology for paginationInformation:
Laravel 10.4 now permits builders to outline a macro for paginationInformation, making it attainable to customise pagination info with out the necessity to lengthen all assets as a base useful resource.
class ResourceCollectionMixin
{
public operate paginationInformation(): Closure
{
return fn ($request, $paginated, $default) => accumulate($default)->mapWithKeysRecursively(fn ($merchandise, $key) => [Str::camel($key) => $item])->toArray();
}
}
These new options, together with quite a few fixes and enhancements, make Laravel 10.4 a considerable replace to the framework. To be taught extra concerning the modifications in Laravel 10.4, seek the advice of the changelog and the excellent record of latest options and updates on GitHub.
Associated