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

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

domain.com/parent_category/child_1/child_2/child_3/child_N/item_id

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

domain.com/itemType/item 

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. http://laravel.io/forum/08-18-2014-should-i-rely-on-an-abstract-structure-when-doing-a-php-application

Last updated 2 years ago.
0

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.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

dwalls32 dwalls32 Joined 18 Aug 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.

© 2024 Laravel.io - All rights reserved.