0

I have tried this

 <div id="keybrandsapp">
    <profile :userdata="'{!! json_encode($user) !!}'"></profile>
 </div>

NB: $user is passed from the laravel controller when requesting for profile view that is

public function profile(){
    $user = Auth::user();

    return \view('user.profile', compact('user', $user));

 }

In my profile component(vuejs code)

    export default {
    props: ['userdata'],
    mounted() {
        console.dir(this.userdata)

    }
}

But this doesnt pass the user data but prints it on the screen.

How do i pass the userdata from laravel blade to vuejs component.

3
  • I'm not a fan of vuejs, how about making a request from vuejs to fetch current user /yourserver.com/user instead of showing the data on page Commented Aug 3, 2017 at 8:29
  • 2
    ` :userdata="'{!! json_encode($user) !!}'"` why are you putting the single quotes between the double quotes and how about using just :userdata="{{json_encode($user)}} Commented Aug 3, 2017 at 8:30
  • @MerhawiFissehaye I see my stupid mistake now the data is getting passed. Commented Aug 3, 2017 at 8:35

1 Answer 1

2

What I suggest is to call the request from the Vuejs template rather than trying to load the data directly into the Html. So the steps will be

  1. Blade loads Vuejs component (at this step you are not getting a user data yet)
  2. component make a request to get user data
  3. component process a returned data
Sign up to request clarification or add additional context in comments.

2 Comments

Exactly what I suggested in my comment above. Displaying json object on page is not cool, even if its hidden behind html.
@Digitlimit while I was typing my answer, this question had 0 comments :) It is, of course, possible to do the way it is stated in a question, however it is not recommended

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.