A
A
Analka2020-05-29 15:06:11
Laravel
Analka, 2020-05-29 15:06:11

Nested categories in Laravel?

Please tell me how to display nested categories correctly and in order not to load the database?
there is a category
5ed0fa4b772b9443237275.png
table a category description table for each language
5ed0fa6bce1d5581803301.png
Category model

protected $table = 'categories';

    protected $fillable = [
        'image','parent','sort_order','published'
    ];

    public function children()
    {
        return $this->hasMany(Category::class, 'parent');
    }

    public function description(){
        return $this->hasMany(CategoryDescription::class, 'category_id');
    }


CategoryDescription Model

protected $table = 'category_descriptions';

    protected $fillable = [
        'category_id','language_id','name','description','meta_title','meta_description','meta_keyword'
    ];

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
lsmacox, 2020-05-29
@lsmacox

You have everything well organized. The only thing I highly recommend is https://github.com/GeneaLabs/laravel-model-caching. And so everything is fine with you.

A
Antonio Solo, 2020-05-29
@solotony

in order not to load the database when sampling, use MPTT (Modified Preorder Tree Traversal)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question