Reputation: 12598
I have an object in laravel 5.5 $categories that looks like this...
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => App\Category Object
(
[table:protected] => categories
[primaryKey] => id
[attributes:protected] => Array
(
[id] => 22
[title] => Fruit
[slug] => fruit
)
[original:protected] => Array
(
[id] => 22
[title] => Fruit
[slug] => fruit
)
)
)
)
How can I get the id from this object? I have tried both of these...
{{$categories->id}}
{{$categories->category->id}}
But they are not working, how should I be extracting this value?
Upvotes: 1
Views: 389
Reputation: 163768
Because it's a collection and one model, you should use pluck()
to get all IDs:
$ids = $categories->pluck('id');
@foreach ($ids as $id)
{{ $id }}
@endforeach
Or you can get them one by one:
@foreach ($categories as $category)
{{ $category->id }}
@endforeach
Upvotes: 1