V
V
Vladimir Kokhan2019-09-08 11:30:22
Laravel
Vladimir Kokhan, 2019-09-08 11:30:22

How to display a record in json format?

There is an entry in the database in JSON format
5d74bbcf999ac416775687.png
Trying to Display in a loop

@foreach($products as $item)
    <p>{{ $item->product_id }}</p>
    <p>{{ $item->value->price }}</p>
@endforeach

gives an error - "Trying to get property of non-object"
What could be wrong?
Thanks

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey delphinpro, 2019-09-08
@SkazochNick

In general, single quotes are not allowed in json. There will be an error. Perhaps the cast suppresses decoding errors and you do not see them, and the output after the cast is null due to an error.
Try changing the single quotes in the base to double quotes.
Also, if the database version allows, you can use the JSON type for the field
5d74c7af00452024649062.png

A
Andrey Suha, 2019-09-08
@andreysuha

{{ json_decode($item->value, true)['price']}}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question