Blade Echo Handlers, Convenient Model Broadcasting, and Download Assertions Added in Laravel 8.45

News

June 4th, 2021

laravel8.jpg

The Laravel team released v8.45.0 with custom Blade echo handlers, model broadcasting, download assertions, and the latest changes in the 8.x branch:

Custom Blade Echo Handlers

Luke Downing contributed custom Blade echo handlers, which gives you the ability to echo out a string representation of an object where you don't have control over the __toString() magic method.

Here's an example from the documentation to help illustrate how this works:

1use Money\Money;
2
3Blade::stringable(function (Money $money) {
4 return $money->formatTo('en_GB');
5});

With the "stringable" callback registered, you can echo out the object in a Blade template as expected with any object using the custom closure instead of __toString() on the object:

1Cost: {{ $money }}

Check out the custom echo handlers for complete details on using this feature.

Model Broadcasting

Taylor Otwell contributed more convenient Model broadcasting features, which introduces the ability to indicate that an Eloquent model should automatically broadcast state changes:

It is common to broadcast events when your application’s Eloquent models are created, updated, or deleted. Of course, this can easily be accomplished by manually defining custom events for Eloquent model state changes and marking those events with the ShouldBroadcast interface.

However, if you are not using these events for any other purposes in your application, it can be cumbersome to create event classes for the sole purpose of broadcasting them. To remedy this, Laravel allows you to indicate that an Eloquent model should automatically broadcast its state changes.

To use this feature, your models will implement the BroadcastsEvents trait and will work in tandem with a broadcastOn() method:

1namespace App\Models;
2
3use Illuminate\Broadcasting\PrivateChannel;
4use Illuminate\Database\Eloquent\BroadcastsEvents;
5use Illuminate\Database\Eloquent\Factories\HasFactory;
6use Illuminate\Database\Eloquent\Model;
7
8class Post extends Model
9{
10 use BroadcastsEvents, HasFactory;
11
12 /**
13 * Get the user that the post belongs to.
14 */
15 public function user()
16 {
17 return $this->belongsTo(User::class);
18 }
19
20 /**
21 * Get the channels that model events should broadcast on.
22 *
23 * @param string $event
24 * @return \Illuminate\Broadcasting\Channel|array
25 */
26 public function broadcastOn($event)
27 {
28 return [$this, $this->user];
29 }
30}

Check out the model broadcasting documentation for complete details for this feature.

Download Assertions

Tanmay Mishu contributed the ability to assert that a test response is a download:

1// Assert the response returned a download
2$response->assertDownload();
3
4// Assert the response is a download with a given file name.
5$response->assertDownload('invoice.pdf');

This feature makes it easier to test different types of file responses from Response::download, BinaryFileResponse, or Storage::download. Check out the assert download in the testing documentation.

Learn More

You can see the complete list of new features and updates below and the diff between 8.44.0 and 8.45.0 on GitHub.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.