Rediska's avatar

How to display the value of a collection?

Please help me with a stupid question I am getting data like this:

$features = $products->pluck('features')
             ->unique()
             ->filter(function($value, $key) {
                 return $value != '[]';
             });

Using foreach I iterate over the results:

@foreach($features as $feature)
             {{dd($feature)}}
@endforeach
Illuminate\Database\Eloquent\Collection {#1784 ▼
  #items: array:1 [▼
    0 => App\Models\Feature {#1823 ▼
      #fillable: array:9 [▶]
      -//-
      #attributes: array:12 [▼
        "id" => 5
        "title" => "buttons"
        "created_at" => "2023-05-27 18:04:36"
        "updated_at" => "2023-05-28 08:15:50"
        "deleted_at" => null
      ]
      -//-

So far everything is great. But until I display the title

@foreach($features as $feature)
             {{dd($feature->title)}}
@endforeach

I am getting this kind of error. What am I doing wrong?

Exception Property [title] does not exist on this collection instance.

0 likes
19 replies
aruszala's avatar

@rediska Try

@foreach($features as $feature)
    {{ dd($feature['title']) }}
@endforeach
1 like
vincent15000's avatar

Why are you trying to use dd() in blade ?

@foreach($features as $feature)
	{{ $feature->title }}
@endforeach

What is strange is the error message : that's as if you were trying to get the title property on the collection instead of on the model.

deansatch's avatar

$feature is an array so {{dd($feature[0]->title)}} would return your title

2 likes
Rediska's avatar

@deansatch It seems to work out like this. But I don't understand why it's an array? Even at the top it says that this is a collection.

1 like
aruszala's avatar

@Rediska the result is a collection. Read further and you'll se the items in it are of type array.

1 like
deansatch's avatar
Level 24

@Rediska sorry, it is a collection but your array is in that collection. So you are looping through the one item in the collection, then trying to call the property on that single collection, whereas you need to get the first() from your collection then loop through the items instead

2 likes
deansatch's avatar

This would probably work better for you

@foreach($features->first() as $feature)
    {{ dd($feature->title) }}
@endforeach
1 like
deansatch's avatar

@Rediska yes because you are using dd() in your loop so it dies at the first result. I assumed you knew that and were just using dd() for debugging. Change it to echo $feature->title and you will see them all

1 like
Rediska's avatar

@deansatch =)))))))))) Naturally, I removed dd))) I'm not that stupid) But it still only returns 1 result

1 like
deansatch's avatar

@Rediska in that case maybe you have multiple items in the collection and only 1 array item in each?

try

@foreach($features as $feature)
	@foreach($feature as $item)

    	{{ dump($item->title) }}	
	@endforeach

@endforeach

Not pretty but might shed some light

1 like
Rediska's avatar

@deansatch And you're right)) It really works. But I have a question. If we nevertheless decided that this is not a collection, but an array in the collection, then why do we refer to these elements as a collection through -> ?))) I can’t understand)

1 like
Snapey's avatar

@Rediska collections ARE arrays, arrays that can contain objects

Or I should say its an object that stores all the data as an array.

2 likes
Rediska's avatar

@deansatch I want to ask another question from the same series. I use ->unique to get unique values but it doesn't work. Since it is applied to the first collection. And I need to sort the unique ones in the nested one. How to do it?

$features = $products->pluck('features')
             ->unique()
             ->filter(function($value, $key) {
                 return $value != '[]';
             });

Please or to participate in this conversation.