2

I am passing a json encoded data to my Vue component as a prop, when I am printing the whole prop variable then it is showing that the data has successfully received, but I am not able to parse the data.

profile.blade.php

@extends('layouts.app')

@section('content')

    <my-profile user-details="{{ json_encode($userDetails) }}"></my-profile>

@endsection

MyProfile.vue

<template>
    <div class="container">
        <div class="row justify-content">
            <div class="col-md-3" id="profile-image">
                <img class="img-fluid rounded" src="https://www.w3schools.com/bootstrap4/paris.jpg" alt="Profile Image">
            </div>            
            <div class="col-md-12">
                <p>{{userDetails}}</p>
                <p> Name:  {{ userDetails.first_name }} </p>
            </div>
        </div>
    </div>
</template>

<style>
#profile-image {
margin-bottom: 30px;
}
</style>

<script>
export default {
props: ["userDetails"]
}
</script>

Output

    {"user_id":2,"first_name":"Shan","last_name":"Biswas","email":"[email protected]","phone":"9508168983","created_at":"2019-05-03 05:43:17","updated_at":"2019-05-03 05:43:17"}

    Name:
3
  • What you tried for parse data? & where you want this parsed data, show unworkable code Commented May 3, 2019 at 9:41
  • Did you try to use json_decode() to get your data in your Vue template ? Commented May 3, 2019 at 9:52
  • The props userDetails is receiving as an string, I have to convert it using JSON.stringfy(). I am not able to find out how to do Commented May 3, 2019 at 9:58

3 Answers 3

5

You need to bind the value to the component as dynamic and not static. So change your blade file to:

@extends('layouts.app')

@section('content')
    <!-- see the colon in front of the props, that is a shortcut for v-bind:prop-name -->
    <my-profile :user-details="{{ json_encode($userDetails) }}"></my-profile>

@endsection

Otherwise are all values passed to the component as a simple string.

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

Comments

1

Change this:

<my-profile user-details="{{ json_encode($userDetails) }}"></my-profile>

with this:

<my-profile user-details='@json($userDetails)'></my-profile>
// Pay attention to single quotes instead of double

This worked for me.

1 Comment

Nope, not working. [Vue warn]: Invalid prop: type check failed for prop "userDetails". Expected Object, got String with value "{"first_name":"Shan","last_name":"Biswas"}". found in ---> <MyProfile> at resources/js/components/MyProfile.vue
0

Update profile.blade.php to following!

@extends('layouts.app')

@section('content')

    <my-profile user-details="{{ $userDetails }}"></my-profile>

@endsection

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.