avatarREUBEN SHUMBA

Summary

Laravel 10.43 introduces streamed JSON responses, a new Eloquent insertOrIgnoreUsing method, schema index methods, class names for global scopes, and a new Artisan command storage:unlink.

Abstract

The latest Laravel update, version 10.43, brings several significant enhancements to improve the development experience. It introduces streamed JSON responses, leveraging the StreamedJsonResponse from Symfony 6.3, which is particularly beneficial for handling large data payloads with reduced memory usage. The update also includes a new Eloquent method insertOrIgnoreUsing for bulk insert operations that can bypass key conflicts. Additionally, Laravel 10.43 adds schema methods for managing indexes, such as hasIndex, and allows for the use of class names when adding global scopes to models, streamlining the process. Lastly, a new Artisan command storage:unlink is introduced to efficiently delete symbolic links.

Opinions

  • The Laravel team is recognized for consistently delivering powerful updates, making the framework more robust and versatile.
  • Streamed JSON responses are praised for their efficiency in handling large data payloads, using less memory and allowing for faster processing.
  • The insertOrIgnoreUsing method is seen as a valuable tool for developers to facilitate bulk inserts and handle key conflicts gracefully.
  • The new schema methods for index management are considered convenient and useful for developers working with database indexes.
  • The ability to use class names when adding global scopes is appreciated for providing a cleaner and more organized approach to managing scopes.
  • The storage:unlink Artisan command is highlighted as a handy tool for managing storage links, simplifying the process for developers.

Laravel 10.43 Introduces Streamed JSON Responses

Designed via Canva

The Laravel team has once again delivered a powerful update with version 10.43, introducing several noteworthy features and improvements. This version packs enhancements that cater to the needs of developers, making Laravel even more robust and versatile.

This now supports assigning a class name to register a global eloquent scope, a new insertOrIgnoreUsing Eloquent method.

Let’s delve into the key highlights of Laravel 10.43

1. Support for Streamed JSON Responses

In Symphony 6.3, the StreamedJsonResponse was introduced, which offers a solution for handling large data payloads more efficiently by reducing memory usage.

Example usage in a controller:

namespace App\Http\Controllers;

use Generator;
use App\Models\User;

class ExampleController extends Controller
{
    public function index()
    {
        return response()->streamJson([
            'users' => $this->yieldUsers(),
        ]);
    }

    protected function yieldUsers(): Generator
    {
        foreach (User::query()->cursor() as $user) {
            yield $user;
        }
    }
}

Additionally, a new test helper is introduced to assert streamed JSON content:

$response->assertStreamedJsonContent([
    'data' => [
        ['id' => 11],
        ['id' => 12],
        ['id' => 13],
    ],
]);

The main benefits of streamed responses are that it uses a lot less memory and can be a lot faster, especially for very large responses. You can yield data that flushes straight into the response stream and the client can start to process the data before all the data is ready and sent on the server side.

You can learn more about this Symphony feature from their HttpFoundation Component documentation for Streaming a JSON Response.

2. New Eloquent insertOrIgnoreUsing Method

Let’s talk about the new insertOrIgnoreUsing method for Eloquent, facilitating bulk insert using a sub-select. This method succeeds even if key conflicts occur, specifically designed for connectors supporting the 'ignore' functionality.

Example usage

$result = $builder->from('table1')->insertOrIgnoreUsing(
    ['foo'],
    function (Builder $query) {
        $query->select(['bar'])->from('table2')->where('foreign_id', '=', 5);
    }
);

// MySQL:
// insert ignore into `table1` (`foo`) select `bar` from `table2` where `foreign_id` = ?'// MySQL: insert ignore into `table1` (`foo`) select `bar` from `table2` where `foreign_id` = ?'

3. New Schema hasIndex() Method

The new Schema::hasIndex() method along with other methods allows for convenient operations related to indexes.

Usage examples include checking for an index, listing tables, and getting index listings.

Schema::hasIndex('users', 'my_index_name');
Schema::hasIndex('users', ['email']);
Schema::hasIndex('users', ['email'], 'unique');
// ... and more
Schema::getTableListing(); // string[]
Schema::getIndexListing('users'); // string[]

4. Use Class Names When Adding Global Scopes

Now, you can pass a scope class name when adding a global scope to a model. This provides a cleaner and more organized approach to managing global scopes.

Example

protected static function booted(): void
{
    static::addGlobalScope(AncientScope::class);
}

Also, the introduction of addGlobalScopes() method, enables the registration of multiple global scopes at once.

protected static function booted(): void
{
    static::addGlobalScopes([FirstScope::class, SecondScope::class]);
}

5. New Artisan Command: storage:unlink

Here is a handy Artisan command:

php artisan storage:unlink

This command efficiently deletes all symbolic links, providing a straightforward solution for managing storage links.

Release notes

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

Conclusion

Laravel 10.43 enhances the development experience with these impactful features and improvements. Whether you are dealing with large data payloads, managing indexes, or utilizing global scopes, Laravel continues to provide elegant solutions. Now, you can explore the complete list of updates and features on GitHub.

Laravel
Laravel 10
Laravel Framework
Laravel Features
Recommended from ReadMedium