1

I am getting odd unexpected behavior that I can't seem to find a solution for, and this is the second time it has happened.

Whenever I try to pass Laravel data to a Vue component via props, instead of assigning the value to the prop, it seems to dump the data out to the DOM, and then the component never renders.

In a .vue single file component, I define my props like so:

props: {
    businesses: {
        type: Array,
        required: true
    },
},

In my Laravel Controller, I have the data like so

$a = new \stdClass();
$a->name = 'tester';
$a->id = 5;
$a->desc = "This is getting a bit ridiculous now";

$b = new \stdClass();
$b->name = 'Boo ya';
$b->id = 2;
$b->desc = "Certainly really annoying to me";
$testStuff = [$a, $b];

return view(
    "businessListing",
    compact('testStuff')
);

In my Laravel Blade template, I have my component setup like this:

<business-listing :businesses="@json($testStuff)"></business-listing>

Also tried this, even though should be the same, and it provided same result.

<business-listing :businesses="{!! json_encode($testStuff) !!}"></business-listing>

Then instead of the component being rendered, I see a mashed up version of the json string, the mounted() function never fires, but also no errors in console.

":"tester","id":5,"desc":"this="" is="" getting="" a="" bit="" ridiculous="" now"},{"name":"boo="" ya","id":2,"desc":"certainly="" really="" annoying="" to="" me"}]"="">

Component Error

If I pass in an empty array, the component renders, mounted() fires as expected, and dumps the empty array to console.

Component Success

Has anybody else run into this? I cannot figure out what is wrong for the life of me. I should mention - Laravel 5.7/PHP 7.1, Vue 2.5.17. Thanks for your help.

0

2 Answers 2

2

You're missing the single quotes around '{ ... }':

<business-listing :businesses="'{!! json_encode($testStuff) !!}'"></business-listing>
Sign up to request clarification or add additional context in comments.

1 Comment

Hey thanks for your answer, I gave it a shot. But unfortunately that does not seem to be the problem, still seeing the same issue.
2

@DigitalDrifter's answer inspired me to mess with the quotes.

It seems that having the first set of double quotes "" was the problem?

If I just use the single quotes, that seems to work. I am not sure why that is the case, any other examples I have looked at, seem to have both, as DigitalDrifter's shows. Working example below.

<business-listing :businesses='{!! json_encode($testStuff) !!}'></business-listing>

1 Comment

The array does not need an extra encoding.. {!! $testStuff !!}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.