Skip to content

Commit 513f9ea

Browse files
add children [sub categories]
1 parent 0f552da commit 513f9ea

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/Models/Category.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Pharaonic\Laravel\Categorizable\Models;
44

55
use Illuminate\Database\Eloquent\Model;
6+
use Pharaonic\Laravel\Categorizable\Models\Categorizable as ModelsCategorizable;
67
use Pharaonic\Laravel\Categorizable\Traits\Categorizable;
78
use Pharaonic\Laravel\Translatable\Translatable;
89

@@ -40,10 +41,22 @@ class Category extends Model
4041
*/
4142
public static function booted()
4243
{
43-
foreach(config('Pharaonic.categorizable.children') as $name => $modelNamespace) {
44+
foreach (config('Pharaonic.categorizable.children') as $name => $modelNamespace) {
4445
static::resolveRelationUsing($name, function ($model) use ($modelNamespace) {
4546
return $model->morphedByMany($modelNamespace, 'categorizable');
4647
});
4748
}
4849
}
50+
51+
public function children()
52+
{
53+
return $this->hasManyThrough(
54+
Category::class,
55+
ModelsCategorizable::class,
56+
'category_id',
57+
'id',
58+
'id',
59+
'categorizable_id'
60+
);
61+
}
4962
}

0 commit comments

Comments
 (0)