The confusion when using slug
in getCustomPath
#276
-
Hi, First of all Happy New Year, thank you for this amazing package and other contributions to the community. Wish you all the best in 2025. I have this data inside my table:
Produced by this simple migration: Schema::create('categories', function(Blueprint $table) {
$table->id();
$table->unsignedBigInteger('parent_id')->nullable();
$table->string('name');
$table->string('slug');
$table->timestamps();
}); I also have my Category model set up with getCustomPath from the documentation: use \Staudenmeir\LaravelAdjacencyList\Eloquent\HasRecursiveRelationships;
public function getCustomPaths()
{
return [
[
'name' => 'slug_path',
'column' => 'slug',
'separator' => '/',
'reverse' => true,
],
];
} Among list of features I want to build on top of this library is to generate a breadcrumb. The idea was for each ancestors of the current active category I want to extract their slug_path fields for my frontend. $categoryB = \App\Models\Category::find(2)
$paths = $categoryB->ancestorsAndSelf()->breadthFirst()->get()
// $paths will yield back this structure:
[
{
id:1,
parent_id: null,
name: 'Category A',
slug: 'category-a',
depth: -1,
path: '2.1',
slug_path: 'category-a/category-b',
},
{
id:2,
parent_id: 1,
name: 'Category B',
slug: 'category-b',
depth: 0,
path: '2',
slug_path: 'category-b',
},
] The order is correct but given Category B is under Category A, shouldn't it be: Category A's ? I see this across the relationship methods. The only method that work is I am using Laravel 11 on PHP 8.3.15 with PostgreSQL 15, but as far as I remember I also got this confusion when working with MySQL (I think what I've done in the past is just loop through the list and extract the Thank you! |
Beta Was this translation helpful? Give feedback.
Hi @aphrxia,
Please see #219.