5

Suppose this is a JS code and I want to print the value of id . Here id is a variable with a value receiving into the jq function.

function follow_or_unfollow(id,action){         
     myUrl = "{{ action('FollowsController@show', 'id') }}" ;               
}

If we mension 'id' it will show the id string.

PS. here {{ is using as it meant as php code in blade template.

Now i need to print the script variable inside a php code.

Ankit Tyagi
  • 2,381
  • 10
  • 19
Sagiruddin Mondal
  • 5,407
  • 2
  • 29
  • 44

3 Answers3

2

I am afraid that what you are asking is not possible. The simple reason being JavaScript is client-side and PHP is server-side.

You can always place PHP code inside JavaScript because it will just be echoed there and won't run at that very instant. Whereas if you want to use a JavaScript variable inside PHP, that means you are trying to access a variable which doesn't even exist at the point of when the server processes the request.

Update

There is a workaround to this. You can pass the data which is needed in JS as parameters in the URL and then get these values in PHP using $_GET.

Aniket
  • 9,622
  • 5
  • 40
  • 62
  • 1
    Thankyou for your kind reply sir. yes then it is a huge problem for me at the present time where I am trying to solve a problem like [THIS-CLICK ME](http://stackoverflow.com/questions/20163499/follow-and-unfollow-using-single-button-with-jquery-ajax-by-laravel-controlle/20168554?noredirect=1#comment30079541_20168554). – Sagiruddin Mondal Nov 24 '13 at 17:10
  • Sir thank you ! But I have already tried it. It cant solve this as this value should be received at the controller in the form of controller call not the url. – Sagiruddin Mondal Nov 24 '13 at 17:15
  • @SagiruddinMondal Then it really isn't possible because it's against the general rule of the languages. – Aniket Nov 24 '13 at 17:16
  • Sir there must be a concatination function inside the js tag ? like myUrl = "+id; – Sagiruddin Mondal Nov 24 '13 at 17:24
  • @SagiruddinMondal I am not clear with what you are asking here. You want PHP inside JS, or the other way round? – Aniket Nov 24 '13 at 17:28
1

Probably its late, but I think I have solution. Your code

myUrl = "{{ action('FollowsController@show', 'id') }}" ;

is inside javascript block. Therefore the blade parser use parenthesis as javascript object rather as blade variable. So you can't use any PHP variables inside script block. In my solution I would replace your line with:

myUrl = "@include("public.default.js_values.myUrl")" ;

and then create that template file

(in your views folder)/public/default/js_values/myUrl.blade.php

and add one line into that file

{{ action('FollowsController@show', 'id') }}

This way blade parser will include the content on myUrl.blade.php. In file will correctly resolve that in parenthesis is variable and returns the value of php variable (in your case the method) back to javascript.

I have tested this in similar situation and it worked.

99problems
  • 534
  • 10
  • 22
1

Just use {!! $vars !!}.

var link = {!! "'".url('string/'.$vars.'/string')."'" !!};
user1709694
  • 322
  • 4
  • 5