DEV Community

Cover image for Laravel 10.20 Released
S-Nidhin
S-Nidhin

Posted on

2

Laravel 10.20 Released

This week, the Laravel team introduced v10.20, featuring noteworthy additions such as a createOrFirst() method, a benchmarking enhancement for a single callable, a novel response JSON assertion, and more:

Eloquent’s createOrFirst() Method:
Tony Messias has contributed a createOrFirst() method, which endeavors to create a new record. If a unique constraint violation occurs, this method strives to locate the corresponding record:

$result = Model::createOrFirst(
[‘email’ => ‘user@example.com’], // Attributes to find
[‘name’ => ‘Example User’] // Values merged with attributes if created
);
Using Trashed Relationship Even Without Soft Deletes:
Mior Muhammad Zaki introduced updates that support using withTrashed(), withoutTrashed(), and onlyTrashed() on a morph relationship:

This enhancement proves beneficial for certain third-party packages, addressing situations where defining the relationship like $this->morphTo()->withTrashed() wasn’t possible unless all relationships utilized the SoftDeletes trait. For more details, refer to the description of Pull Request #47880.

Benchmarking a Single Callable and Retrieving a Result:
Tim MacDonald has added a value() method to the benchmark utility class. This method permits measuring a given callable inline and obtaining the resultant value:

public function list()
{
[$response, $duration] = Benchmark::value(
fn () => Http::get(‘https://external-service.com/list.json')
);

if ($duration > 1000) {
Log::warning('external-service.com is running slow');
}

return $response->json();
}
Introducing Default Values for Merging Data into an API Resource:
Choraimy Kroonstuiver has contributed the ability to provide a default value to mergeWhen(). This enhancement simplifies logic involving mergeWhen() and mergeUnless() when supplying defaults:

Before Laravel 10.20:

class MyResource extends JsonResource
{
public function toArray($request): array
{
return [
/* other properties /
$this->mergeWhen($this->hasSpecificData(), $this->specificData()),
$this->mergeUnless($this->hasSpecificData(), static::genericData()),
];
}
}
**After Laravel 10.20
*:

class MyResource extends JsonResource
{
public function toArray($request): array
{
return [
/* other properties /
$this->mergeWhen($this->hasSpecificData(), $this->specificData(), static::genericData()),
];
}
}
**Canonical JSON Path Assertion
*:
Günther Debrauwer has introduced an assertJsonPathCanonicalizing() method, which allows verifying if a JSON response encompasses all anticipated values without considering their order:

$users = User::factory()->count(2)->create();
$response = $this->get('/api/users');
$response->assertJsonPathCanonicalizing(
'data.*.id',
$users->pluck('id')->all()
);
Moreover, Günther Debrauwer’s addition of the assertJsonPathCanonicalizing() method underscores Laravel’s commitment to facilitating efficient and reliable testing processes. These updates collectively underscore the continued growth and adaptability of Laravel within the context of modern web and Laravel app development, reinforcing its status as a robust and forward-looking framework.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay