2

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])

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

-1

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

Comments

Your Answer

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