0

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?

1
  • right square brackets missing someFunc({{$rt['title']}}) in function parameter Commented Nov 23, 2021 at 11:20

2 Answers 2

1

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

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

1 Comment

It won't work if you have a single quote or double quote in the title. Also it might allow code injection. This code is not safe.
1

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

1 Comment

this also worked too. thanks

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.