I am using Laravel Blade format for my HTML and I am passing multiple PHP variables to JavaScript functions. The issue is that the variables being passed to JavaScript function could have special characters.
I want the JavaScript function to be as follows:
jsFunction("argument1","argument2","argument3")
What I get:
jsFunction('argument1','argument2','argument3')
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>
The best shot is to not reinvent the wheel and try searching :)
There is a package already on github https://github.com/laracasts/PHP-Vars-To-Js-Transformer
It does exactly what you want. Its for Laravel 5 as well as Laravel 4.
Here is an example of how I am doing it. I have a form with an update button which calls a JavaScript function.
{!! Form::button('Update', array('class' => 'btn btn-success', 'onClick' => 'updateTeamName(' . $team_id . ')')) !!}
I am passing the $team_id variable (passed to the view by the controller method) to the JavaScript function.
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With