user3036307
user3036307

Reputation:

Laravel Blades - Assign JS variable to PHP variable

Is it possible to assign javascript variable to php variable?

Defined: {{ $var }} and @{{ jsVar }}

I'd like to something like this (this example not working): {{ $var = jsVar }}.

This is needed to pass the parameter to e.g. generate named routes. I can't do something like that route('user', ['id' => jsVar])

Upvotes: 2

Views: 2106

Answers (2)

Rob Fonseca
Rob Fonseca

Reputation: 3819

No, it is not possible. PHP is a server side language and executed before you even see the HTML output. JS is client side and runs after the server side code has been executed.

You will need to rethink your architecture and call a API endpoint via AJAX from JS so you can talk to the server side of your application

Upvotes: 3

tokkerbaz
tokkerbaz

Reputation: 397

I usually use like this, in order to send some variable to js function myCustomFunction({{$laravel_var}})

Upvotes: -1

Related Questions