Back to Blog

Laravel 10.4 Released

Julian Beaujardin
Julian Beaujardin Reference: Laravel News March 22nd, 2023

The Laravel team released 10.4 with a File::json() method, converting existing HasMany relationships to a HasOne relationship, a new test response assertion, and more.

File::json() method

Austin White contributed a File::json() method as a convenience for getting JSON encoded data from a file:

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

// After
$data = File::json('sample.json');

Assert unsupported media type

Shamimul Alam contributed an assertion helper for the 415 Unsupported Media Type response status code:

$response->assertUnsupportedMediaType();

Convert an existing HasMany to HasOne relationship

Luke Kuzmish contributed converting a HasMany to a HasOne and a MorphMany to MorphOne.

Take this example of having to define two relationships:

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();
    }
}

With this PR now, you can do the following instead with the ->one() method:

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();
    }
}

The one() method is available on the HasMany, HasManyThrough, and MorphMany.

Create macroable method for: paginationInformation

Frans Slabbekoorn contributed the ability to define a macro for paginationInformation that allows customizing pagination information without having to extend all resources as a base resource:

/** @mixin \Illuminate\Http\Resources\Json\ResourceCollection */
class ResourceCollectionMixin
{
    public function paginationInformation(): Closure
    {
        return fn ($request, $paginated, $default) => collect($default)->mapWithKeysRecursively(fn ($item, $key) => [Str::camel($key) => $item])->toArray();
    }
}

Release Notes

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

Unreleased

v10.4.1 (2023-03-18)

Changed

  • Move Symfony events dispatcher registration to Console\Kernel (#46508)

v10.4.0 (2023-03-17)

Added

  • Added Illuminate/Testing/Concerns/AssertsStatusCodes::assertUnsupportedMediaType() (#46426)
  • Added curl_error_code: 77 to DetectsLostConnections (#46429)
  • Allow for converting a HasMany to HasOne && MorphMany to MorphOne (#46443)
  • Add option to create macroable method for paginationInformation (#46461)
  • Added Illuminate/Filesystem/Filesystem::json() (#46481)

Fixed

  • Fix parsed input arguments for command events using dispatcher rerouting (#46442)
  • Fix enums uses with optional implicit parameters (#46483)
  • Fix deprecations for embedded images in symfony mailer (#46488)

Changed

  • Added alternative database port in Postgres DSN (#46403)
  • Allow calling getControllerClass on closure-based routes (#46411)
  • Remove obsolete method_exists(ReflectionClass::class, 'isEnum') call (#46445)
  • Convert eloquent builder to base builder in whereExists (#46460)
  • Refactor shared static methodExcludedByOptions method to trait (#46498)