K
K
Knyashshsh2018-04-27 22:21:54
Laravel
Knyashshsh, 2018-04-27 22:21:54

How to print data?

@foreach ($post->tags as $tag)
  <a href="#" class="main-post-description-line-tag">{{$tag}}</a>
@endforeach

Which brings out
<a href="#" class="main-post-description-line-tag">[{"id":17,"title":"23525246","created_at":null,"updated_at":null}]</a>
<a href="#" class="main-post-description-line-tag">[{"id":18,"title":"674","created_at":null,"updated_at":null}]</a>	
<a href="#" class="main-post-description-line-tag">[{"id":19,"title":"dssdfsdf ","created_at":null,"updated_at":null}]</a>
<a href="#" class="main-post-description-line-tag">[{"id":20,"title":"er","created_at":null,"updated_at":null}]</a>

When using {{$tag->id}} it gives an error
Property [id] does not exist on this collection instance. (View: D:\OpenServer\domains\laravel\resources\views\posts\show.blade.php)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yan-s, 2018-04-27
@fyapy

Because you have an array of objects in $tag.
Do
{{ $tag[0]->id }}
or
{{ $tag->first()->id }}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question