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

stephan-v's avatar

Echo vuejs data inside php function

I currrently want to echo vuejs data inside a php function like so:

{{ auth()->user()->timesAddedBeer(@{{ beer.id }}) }}

But laravel gives me the following error:

syntax error, unexpected '{'

Can anybody tell me the right way to do this?

0 likes
5 replies
oes's avatar

You can't add JS data to a php function.

cleanse's avatar

Is that value stored in your database? If so, just query it in the controller and pass that data to the view...

If not, you can try using ajax to make the auth->user()->timesAddedBeer() call.

var beerId = {{ beer.id }};

$.ajax({
    type: 'GET',
    url:'/get/beer/id/route',
    data: beerId,
    success: function(data) {
        //set value in the dom
    }
});

PHP = Serverside, JS = client side.

stephan-v's avatar

It's a value from Algolia. Anywhere else I am able to echo that value on the page like so:

@{{ beer.id }}

I don't want to add an ajax request when it can be done cleanly. I will try to look for another option.

jekinney's avatar
Level 47

Good luck. You can't mix Mustache binding and php. Another opinion is session or local and call it up via JavaScript.

Please or to participate in this conversation.