How to pass PHP variable to javascript function from html blade format

I am using the laravel blade format for my html and I am passing a few php variables for javascript functions. Now the problem passed to the javascript variable can have special characters. Now I want the javascript function to be as follows:

what I want:

jsFunction("argument1","argument2","argument3")

what i get:

jsFunction('argument1','argument2','argument3')

Now my blade format code is:

<a href="#" onclick="displayBannerInvoice('{{$bannerProperty->property_id}}','{{ $bannerProperty->id }}','{{$bannerProperty->end_date}}','{{$bannerProperty->no_of_days}}','{{$bannerProperty->total}}','{{$bannerProperty->vat_percentage}}')"></a>
+4
source share
3 answers

The best shot is not to reinvent the wheel or try to search :)

There is a package already on github https://github.com/laracasts/PHP-Vars-To-Js-Transformer

He does exactly what you want. Its for Laravel 5 as well as Laravel 4.

+6

php :

{!! $someVar!!}

:

console.log({!! $someVar!!});

0

Here is an example of how I do it. I have a form with an update button that calls a JavaScript function.

{!! Form::button('Update', array('class' => 'btn btn-success', 'onClick' => 'updateTeamName(' . $team_id . ')')) !!}

I pass the $ team_id variable (passed to the view by the controller method) to the JavaScript function.

Hope this helps.

0
source

All Articles