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

also using that structure. I've currently done something like this to check my URL matches my database structure. I don't know if it's the best way to do it. it works it's way back through the parents of the category in the database to see if the slugs combined match the supplied url

eg http://localhost/products/lighting/portable-lights/

(Actually my logic is the wrong way round as I'd need to check if it's a product first then check back through the category chain, but you get the idea)

Route::get('/products/{any?}', 'ProductsController@showProducts')->where('any', '(.*)?');

class ProductsController extends BaseController {

    public function showProducts($hierarchy=null)
    {
        $category = Category::where('slug', end($categories))->first();
    
        // if there's a category found from the slug
        if ($category)
        {
            // some other stuff
            // ...
    
            // time to work backwards thru parent chain	
            if($category->parent)
            {
                do
                {
                    $parent = $category->parent;
                    array_unshift($slugs, $parent->slug);
                } 
                while($parent->parent);
            }
            
            // let's check if the slugs from the database hierarchy match the URL slugs
            $joinedSlugs = join($slugs,"/");
            $valid = ($joinedSlugs==$hierarchy);
        }
       else
       {
            // deal with top level /products case or /product within category later
       }
    }
}
Last updated 9 years ago.
0

Hello jmp909, i have the same interest, need a tree structure for categories width only a "parent_id" field. I don't know how to do, and for the routes too.

I would like to have both : /category/subcategory/product and /category/subcategory/subcategory/product and /category/product ...

Can you say us if you have succeed and how ?

Thank you.

0

are you trying to using friendly URLs? eg

/clothes/footwear/nike-trainer

that's where the fun starts!

essentially I check if the end slug is a product slug that exists in the database, if not i check it it's a category slug, then work my way back up the chain of slugs and check they are what they should be.

although i'm thinking it'd be better to store a subcategory's slug in the database to save on lookup chains

id	category	slug				parent_id
-----------------------------------------------------------------
1	Clothes		clothes				null
2	Footwear	clothes/footwear		1
3	Shoes		clothes/footwear/shoes		2
4	Trainers	clothes/footwear/trainers	2
5	Shirts		clothes/shirts			1
Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

jmp909 jmp909 Joined 29 Jan 2015

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.