laravel category code example
Example: get category hierarchy to the top laravel
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
public function child() {
return $this->hasMany('App\Category', 'parent_category_id');
}
public function children() {
return $this->hasMany('App\Category', 'parent_category_id')
->with('children');
}
public function parent() {
return $this->belongsTo('App\Category', 'parent_category_id');
}
public function parents() {
return $this->belongsTo('App\Category', 'parent_category_id')
->with('parent');
}
}