Support the ongoing development of →
Database Eloquent Architecture
Last updated 2 years ago.

well the schema that you have is correct, with that schema you could define the model like this:

public function category(){
    return $this->belongsTo("Category", "parent");

now for the routing thing it sound to me that you need to traverse the category tree backwards. And I wouldn't have that kind of routing

having to many '/' is bad practice for SEO. Instead I would provide a breadcrumbs navigation for each item, and the routing would be 

easier for the bots to read.

To generate the breadcrumb type just go backwards on that category

//return a collection of categories
public function traverse_backwards(){
    $collection = array();
    $i = $this;
    while($i != NULL)
        //add category to collection
        $collection[] = $i;
        //go back one level
        $i = $i->Category::find($i->parent);
    return $collection;

This code has not been tested, is just for showing the logic behind the solution.

please check my question out.

Last updated 2 years ago.

Thank you so much for your response! That's good insight into what the bots will read. I could keep the url's shorter. I will probably just use a pattern closer to what you suggest.

Thank you!

Last updated 2 years ago.

Sign in to participate in this thread!


Your banner here too?

dwalls32 dwalls32 Joined 18 Aug 2014


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

Your logo here?

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

© 2024 - All rights reserved.