简体   繁体   中英

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

So im my view (made with using Blade templates) I have link with href that calls Controller directly, that looks like:

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

and it works fine, but now I need to pass some additional data , which is taken from vue.js array , which also should be put in curved braces, like:

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

and here I start getting messages about unexpected { symbol . I've tried removing @ symbol, and putting this variable outside href quotes, but it didn't work. Is there any possibility to solve this?

UPD: I also tried putting this link in vue method, like

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

but in this case literally "vm.user.id", not variable gets passed in link

UPD2: I'm now trying to pass data not in link, like $.getJSON('{{action("Controller@method")}}', {user_id: vm.user.id})

and I'm getting links like update?user_id=123 , but I need format like update/123

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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM