I am using laravel 10.
I created the following tables to store and work with categories (based on the wordpress database architecture)
term table schema:
public function up() {
Schema::create('terms', function (Blueprint $table) {
$table->bigIncrements('term_id');
$table->string('name', 200)->nullable();
$table->string('slug', 300)->unique()->index();
$table->timestamps();
});
}
term_taxonomy table schema:
public function up() {
Schema::create('term_taxonomy', function (Blueprint $table) {
$table->bigIncrements('term_taxonomy_id');
$table->bigInteger('term_id')->unsigned();
$table->string('taxonomy');
$table->string('icon')->nullable();
$table->text('description')->nullable();
$table->bigInteger('parent')->unsigned()->default(0);
$table->bigInteger('count')->default(0);
$table->timestamps();
$table->foreign('term_id')->references('term_id')->on('terms')->onDelete('cascade');
});
}
Question:
In Laravel, how can I create hierarchical category URLs like:
domain.com/top-travel/ //parent 1
domain.com/top-travel/sea //child of parent 1
So that the child category (sea) with the parent (top-travel) will have the URL "domain.com/top-travel/sea" instead of just "domain.com/sea"
Please show me how to do this in as much detail as possible. I'm new to Laravel and have tried several approaches but can't seem to get any of them to work properly. I'm trying to fully understand this specific issue with Laravel.
-- Update one --
I have written code somewhat like this: in CategoryController.php
public function show($slug)
{
// Find the post based on the slug
$category = Term::whereSlug($slug)->firstOrFail();
// Get the list of parent categories
$parents = [];
$parent = $category->taxonomy;
// Traverse through parent categories until reaching parent = 0
while ($parent && is_object($parent) && $parent->parent) {
$parents[] = $parent->slug;
$parent = $parent->parent;
}
// If there are parent categories, add them to the URL
$url = empty($parents) ? '' : implode('/', array_reverse($parents)) . '/';
// Check if $parent is an object before accessing the 'slug' property
if (is_object($parent)) {
$url .= $parent->slug . '/';
}
// Add the current category slug to the URL
$url .= $slug;
// If there are no parent categories, use the basic path
if (empty($parents)) {
$url = 'cat/' . $slug;
}
// Get the icon and description values from the term_taxonomy table
$icon = $category->taxonomy->icon;
$description = $category->taxonomy->description;
// Display the post
return view('categories.show', compact('category', 'icon', 'description', 'url'));
}
In this code, I'm attempting to construct the URL by traversing through parent categories and adding their slugs to the URL path. However, the challenge remains in managing the hierarchy and creating the correct URL structure. If you have any suggestions or insights on how to improve this, I would greatly appreciate it!"
On the basis of this document, you will need to define Regex in your routes as follows:
If you then call `domain.com/product/test1/test2', the category parameter in your controller will look like this:
And all you have to do is parse that string to find your category.
Note: Once you have used this method, all routes that come after
productwill be a category and will perform theshowaction in theCategoryController!