Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Daniel-Pablo's avatar

HELP, convert string to json... ?

HI, please I am trying to get out the information of this string

I save this string in a field of my data base

[{"layout":"information","key":"phjxcw98rGBxz9TC","attributes":{"tittle":"bienvenida","video":"23483"}},{"layout":"information","key":"2Wf2vWG0wMwdgkcy","attributes":{"tittle":"ingredientes","video":"1239891283"}},{"layout":"information","key":"w0BOvHFBrUvkSatU","attributes":{"tittle":"recomendaciones","video":"12391283"}}]

when I call this on a laravel view

{{ $seccion->information }}

got the information of the top, BUT HOW I CAN ENTER INTO attributes->tittle ???????????

Thanks in advance

0 likes
9 replies
Romain's avatar

You’re working with an array. You will need [0] somewhere. Use dd() to see what is returned

Daniel-Pablo's avatar

@romain thankss look, I advance a bit , look I uses

json_decode($info);

got this

[
	{
		"layout": "information",
		"key": "phjxcw98rGBxz9TC",
		"attributes": {
		"tittle": "bienvenida",
		"video": "23483"
		}
	},
		{
		"layout": "information",
		"key": "2Wf2vWG0wMwdgkcy",
		"attributes": {
		"tittle": "ingredientes",
		"video": "1239891283"
		}
	},
		{
		"layout": "information",
		"key": "w0BOvHFBrUvkSatU",
		"attributes": {
		"tittle": "recomendaciones",
		"video": "12391283"
		}
	}
]

BUT CANT ACCESSS THE INFO,,,, HOW???? thanks in advance

jamalroger's avatar

@santino use casts in model will convert your json into php arrays Expemple In your model


Protected $casts=[
'information' =>'array' 
] ;

In view

$model->information // return php array 
Daniel-Pablo's avatar

@romain @jamalroger Jussst add the model array cast, so now I don't nee do to convert to Json, but HOW I CAN ENTER INTO THE ARRAY I don't have access I don't know how to get any value...

can you teach me how??

jamalroger's avatar
Level 1

@santino like any php array In the view

@foreach($section->information as $info)
{{$info['key'] }} 
@endforeah
1 like
Daniel-Pablo's avatar

O MY GOD!!! you are a genius @jamalroger this was making me crazy!!! dude!!!!!!! THANK A LOT REALLY APPRECIATE IT ALL DAY TRYING TO GET THE INFO

Thanks, Sr!!

and @romain thanks for your feed back Too

Daniel-Pablo's avatar

got another problem with this...

I use this to make information field an array,

Protected $casts=[
		'information' =>'array'
	] ;

all was perfect working excellent, but I am using laravel NOVA when it goes to save the information into this field as a string it does get a crash error because this got the cast array

So I comment it out, enter into the laravel view, changed the foreach from this

@foreach( $seccion->information as $info)

<div> {{ $info["attributes"]["tittle"] }}</div>

@endforeach

to this

@foreach(   explode( ',' , $seccion->information) as $info)

{{  $info }}

@endforeach

but now I can't get out the information of attributes tittle I get this long text...

[{"layout":"information" "key":"j8rMdCccjHHJI5ih" "attributes":{"tittle":"asdfasfd" "video":"234234"}} {"layout":"information" "key":"X3ClC6W357d44kIC" "attributes":{"tittle":"otro video" "video":"aqui"}} {"layout":"information" "key":"soBDv8tgqLuufYWR" "attributes":{"tittle":"aewer" "video":"wer"}}]

Do you know How I can get the information about these fields?? I got no idea thanks to @jamalroger @romain @jlrdw for helping me in this issue, I am new at this

Please or to participate in this conversation.