Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

number6's avatar

Blade to Vue: Alternative to json_encode

I forget which video it was in, but Jeffrey pointed out another way to pass a Laravel collection to vue. Currently I am using this. What is the other alternative to passing the collection data? Is it just {{ $private}}? Anyone remember what video that was in?

        <basictable
        :data="{{ json_encode($private) }}"
           :columns="{{ json_encode($columns) }}"
    >
    </basictable>
0 likes
3 replies
michaeldyrynda's avatar

If you just pass $private it will automatically be converted to a JSON string.

spekkionu's avatar
Level 48

When printing an object in a blade template the object is attempted to be converted to a string.

Both Eloquent models and Laravel Collection class have a __toString method that converts itself to json so anytime you would echo or print them they are automatically converted into a json string.

This is why you can just to something like {{ $user }} and it will be converted into json.

You can implement a __toString() method on any of your own classes if you want to be able to do that to them as well.

number6's avatar

Thanks guys. Figured that was the case. Just couldn't find the video where Jeffrey mentioned it.

Please or to participate in this conversation.