[Vuejs]-Call laravel {{action(Controller@method}} with passing variable from vue.js array

0👍

I’m really not familiar with vue.js, but in general PHP would execute on server, and javaScript on client. So first line

<a id="@{{user.id}}" href="{{action('Controller@method', ['user_id' => @{{user.id}}])}}>update</a>

has this part @{{user.id}} PHP can’t understand, as it’s javaScript notation, so you get some kind of syntax error.

And for second line

$.getJSON('{{action("Controller@method", ["user_id" => '+vm.user.id+'])}}')

I think that this could not get executed either since this doesn’t load Laravel, and to PHP {{action("Controller@method")}} has no particular meaning without Laravel loaded.

You could do something like this. Hardcoding part of URL that will render from server, and adding rest of the URL when it goes to client.

For example you can have server generate URL like this:

<a id='url' href="http://example.com/controller/method/">update</a> 

And then have some kind of javascript function on client that would append vm.user.id to it when page loads

$(document).ready(function(){
    $('#url').attr('href', $('#url').attr('href') + vm.user.id);
});

I think this would result in having an URL like this

<a id='url' href="http://example.com/controller/method/123">update</a> 

Leave a comment