Support the ongoing development of Laravel.io →
Eloquent Views

Hi for example I have controller and in this we have below code

	$categories=Category::with('parent.parent.parent.parent')->find($category);
	return view('category.detail')->with('categories',$categories);

above data we have parent field upto 4 sub levels but I need unlimited sub levels

from above code output json data like below

{"id":5,"name":"sub sub category","project_id":1,"category_id":4,
	"parent":{"id":4,"name":"sub second","project_id":1,"category_id":3,
		"parent":{"id":3,"name":"floor tiles","project_id":1,"category_id":2,
			"parent":{"id":2,"name":"tiles","project_id":1,"category_id":0,
				"parent":null}}}}

this sub parent may be unlimited

No I want that laravel will check level numbers and according to that it will

My code in views is like below

<ol class="breadcrumb">
 <li class="active">{!!$categories->parent->parent->parent->name!!}</li>
 <li><a href="#">{!!$categories->parent->parent->name!!}</a></li>
  <li><a href="#">{!!$categories->parent->name!!}</a></li>
</ol>

here when there is upto 4 levels is ok, but when sub levels are upto 3 then it shows error "Trying to get property of non-object "

Plz help me

Last updated 3 years ago.
0

Sign in to participate in this thread!

PHPverse

Your banner here too?

arctushar arctushar Joined 5 Oct 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.