Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a PHP variable to Vue component instance in Laravel blade?

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.

like image 354
mastercheef85 Avatar asked Jan 07 '17 10:01

mastercheef85


People also ask

How do I pass data from laravel blade to Vue component?

Simply you can take this in your blade: <span hashid="{{ Auth::user()->id }}"></span> and in your vue component do like: <script> export default { data: function () { return { hashid: '' } } } </script> and console it anywhere, it'll give you auth id!

Can I use Vue with laravel blade?

Laravel provides Vue. js support out of the box which means that we do not have to create a separate Vue project. Instead, we can write our Vue logic directly in the Laravel resources folder. In this article, we will look at how we can write and use Vue components inside our blade files.

How do you pass props in Vue component?

To specify the type of prop you want to use in Vue, you will use an object instead of an array. You'll use the name of the property as the key of each property, and the type as the value. If the type of the data passed does not match the prop type, Vue sends an alert (in development mode) in the console with a warning.


2 Answers

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:

<client-details inline-template client-id="{{ $client->id }}">     <div id="client-details" class="">           My HTML stuff     </div> </client-details> 

In your Vue component:

Vue.component('client-details', {     props: [         {             name: 'clientId',             default:0,         }     ] }); 

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 format clientId="{{ $client->id }} without a colon.

like image 85
Mustafa Ehsan Alokozay Avatar answered Sep 22 '22 01:09

Mustafa Ehsan Alokozay


I just did this and it's working great for me. Using Laravel 5.4 and Vue 2.x.

In my component, declare a property (props) for the object:

props: ['currentUser'],

On my blade page where the component is instantiated:

<my-component v-bind:current-user='{!! Auth::user()->toJson() !!}'></my-component>

Note that in the component, I am using camelCase for currentUser, but because html is case-insensitive, you have to use kebab-case (thus, the 'current-user').

Also note, if you use blade syntax {{ }} then the data between is run through php htmlspecialchars. if you want unencoded data (which in this case you would), then use {!! !!}

like image 31
Hawkeye64 Avatar answered Sep 23 '22 01:09

Hawkeye64