Cutting Eloquent Models with Blade

If you already tried to do something with Blade Components in Laravel, probably you already stumbled upon the many limitations of the subsystems.

Among the many, the latest - and most disappointing - I've found is that seems not possible to pass an Eloquent instance as attribute to a component: executing

<x-my-component :obj="$instance" />

results in a string, an escaped version of the model itself.

The reason is here: as the value of the attribute implements the __toString method, it is always converted into a string.

Except when it is an instance of Illuminate\View\ComponentAttributeBag.

So, if you really want to deliver an Eloquent instance to your Blade Component, you may execute

<x-my-component :obj="new \Illuminate\View\ComponentAttributeBag([$instance])" />

and then, within the Component itself, retrieve it with

$obj = $obj->first();

Chances are that this will be fixed, someday...

Show Comments