xxxxxxxxxx
/*
|========================================================
| Recursive relationships in laravel
|========================================================
*/
// IN CATEGORY MODEL CLASS
class Category extends Model {
// GET PARENT CATEGORY DETAILS
public function parent()
{
return $this->belongsTo('Category', 'parent_id');
}
// GET SUBCATEGORIES LISTING
public function children()
{
return $this->hasMany('Category', 'parent_id');
}
// GET RECURSIVE CATEGORIES
public function childrenRecursive()
{
return $this->children()->with('childrenRecursive');
}
}
// IN CATEGORIES CONTROLLER CLASS
$categories = Category::with('childrenRecursive')
->whereNull('parent_id')
->get();
// ======= THANKS ======= //