1

File Structure

Inside App.blade.php

I used vue router to pull the components.

I have a normal variable like $users which I can access in blade file like {{$users}} . Now how can I access the same data in my component?

1
  • where your component exist ? in .php file or in .js or .vue file ? Commented Dec 14, 2018 at 8:05

3 Answers 3

1

If your $users variable is a collection you can use:

<my-component :users="{!! $users->toJson() !!}"></my-component>
Sign up to request clarification or add additional context in comments.

2 Comments

This will definitely work only when if this component put inside .php file.
I assume that being the case here as App.blade.php is mentioned.
0

Laravel is php server side scripting language which compiled before front end Javascript and Vue is javascript.

So to get value of php variable in js take this value in a input text field as json

<input type="hidden" id="users" value="{{json_encode($users)}}">

And inside vuejs inside mount() update your data's, users element

mounted(){
   var users = document.querySelector("#users").value;
   users = JSON.pars(users);
   this.users = users;
}

Comments

0

For this purpose I am using props, Look at my example :

I have a QuestionComponent and I want to sent $quetion_set->id valuable to the file so that In blade file I sent a props ,

 <question question_set_id="{{$question_set->id}}"/>

In the QuestionComponent I set props : props:['question_set_id'], then used it in the component like this : this.question_set_id

If you want to pass array data then you should use JSON.parse function to reache your data , for example :

   JSON.parse(this.question_set_id)['id']

Comments

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.