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

Filth's avatar

first element of object in blade

My products can belong to many categories. I use a category slug as part of the url. in my dashboard im trying to create a link to edit the product page so based on $product having multiple categories I want it to use the first result.

In Tinker the following works

$product->categories{0}->slug

However it doesn't in my view which I assume is to do with curly braces and blade.

{{ URL::route('product.edit', ['cat_slug' => $product->categories{0}->slug , 'slug' => $product->slug]) }}

The following also throws an error yet is ok in tinker

$product->categories->first()->slug

ErrorException in 9f71a027c69fcef767e74df068f0e9c7 line 50: Trying to get property of non-object (View: /volume1/web/laravel/resources/views/admin/products.blade.php)

0 likes
3 replies
Filth's avatar

Sorry, Im going to put this down to monday morning and not enough caffeine, I just realised currently not every product is assigned to a category so this will fail.

Sorry

JeroenVanOort's avatar

Explaining a problem to someone else has proven me to be a reliable way of realising the small but important mistakes. Sometimes people don't even have to answer ;-)

1 like

Please or to participate in this conversation.