How can I pass a value of a PHP variable to a Vue component in Laravel blade files?
In my example, I have an inline template client-details, I get this view from Laravel
so now I want to pass the id
which comes with the url /client/1
to my Vue
instance.
My component, which is loaded by Laravel
, looks like:
<client-details inline-template>
<div id="client-details" class="">
My HTML stuff
</div>
</client-details>
and mounted by:
Vue.component('client-details', {
data(){
return {
clientId: null
}
},
);
I already tried like
:clientId="{{ $client->id }"
but it does not work.
You have to use Vue's
props
to be able to pass attributes to Vue's components through markup. Take a look at the following example:In your Vue component:
Now in other parts of your Vue component, you can access this value as
this.clientId
.Issue Details
Please note that in HTML we write attribute name in kebab-case but in Vue side we write it in camelCase. More info in official docs here.
Also, you are using Vue's
v-bind
shorthand in:clientId="{{ $client->id }}"
which means that Vue will deal anything inside double quotes as a JavaScript expression, therefore you may get errors in that case as well. Instead, you should use this formatclientId="{{ $client->id }}
without a colon.