Answers for "category hierarchy laravel"

PHP
1

category hierarchy laravel

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    // One level child
    public function child() {
        return $this->hasMany('App\Category', 'parent_category_id');
    }

    // Recursive children
    public function children() {
        return $this->hasMany('App\Category', 'parent_category_id')
          			->with('children');
    }

    // One level parent
    public function parent() {
        return $this->belongsTo('App\Category', 'parent_category_id');
    }

    // Recursive parents
    public function parents() {
        return $this->belongsTo('App\Category', 'parent_category_id')
          			->with('parent');
    }

}
Posted by: Guest on October-15-2020

Code answers related to "category hierarchy laravel"

Browse Popular Code Answers by Language