adeguntoro
7 months ago
1854
3
Laravel

how to access json column in laravel

Posted 7 months ago by adeguntoro

i edit laravel default user migration and i add meta column, and this meta column is where i save my data in json format. But now i'm bit confuse how to access this column. I use standar eloquen to get data from database

$website = User::find(1);
return view('website.index')->with('website',($website));

then in my blade file, i try this

{{ $website->meta->gender }}

and it give me error

Trying to get property 'gender' of non-object

i also try another method from stackoverflow (https://stackoverflow.com/questions/30073065/laravel-how-do-i-parse-this-json-data-in-view-blade) :

$website = User::find(1);
return view('website.index')->with('website', json_decode($website, true));

//blade file
@foreach($website->meta as $member)
    gender: {{ $member['gender'] }}
@endforeach

but also it give me error

Array to string conversion 

and this is my current output if i use return view('my.blade, compact('mydata'))

///{!! $website !!}

email_verified_at: "2020-02-13 12:22:56",
status: "1",
meta: {
gender: "Male",
country: "Antarctica (the territory South of 60 deg S)",
biodata: "The Dormouse had closed its eyes again.",
address: "11481 Kelsie Dale Port Humberto, SC 94117-0111",
parents: {
mother: "Miss Juliana Williamson",
father: "Dell Franecki",
kids: {
number1: "Waldo Schiller",
number2: "Angelita Smith",
number3: "Ladarius Witting"
}
},
skills: [
"JavaScript",
"VueJS"
]
},

and i already cast my meta as array. So, how can i access my json column in laravel ?

Please sign in or create an account to participate in this conversation.