Home > Back-end >  pass php parameters to vue function inside bladee
pass php parameters to vue function inside bladee

Time:11-23

In my blade view.

@foreach($r_templates as $key => $rt)
    <button @click="someFunc({{$rt['title'}})">click me</button>
@endforeach

// In my vue script
someFunc(title) { console.log(title); }

the above code seems to not work and it does not have errors too.

but when I change the parameter to 'something', it works.

How do I pass a PHP variable to a vue function?

CodePudding user response:

as your passing string you need to add ' 'this quote

try this

@foreach($r_templates as $key => $rt)
    <button @click="someFunc( '{{$rt['title'}}' )">click me</button>
@endforeach

CodePudding user response:

You have no quotes arround the parameter/value.

Ensure proper quotes and escaping using the JSON function:

'someFunc({{ Js::from($rt['title']) }})'

Reference: https://laravel.com/docs/8.x/blade#rendering-json

  • Related