category hierarchy laravel

Solutions on MaxInterview for category hierarchy laravel by the best coders in the world

showing results for - "category hierarchy laravel"
Awa
08 Nov 2016
1<?php
2
3namespace App;
4
5use Illuminate\Database\Eloquent\Model;
6
7class Category extends Model
8{
9    // One level child
10    public function child() {
11        return $this->hasMany('App\Category', 'parent_category_id');
12    }
13
14    // Recursive children
15    public function children() {
16        return $this->hasMany('App\Category', 'parent_category_id')
17          			->with('children');
18    }
19
20    // One level parent
21    public function parent() {
22        return $this->belongsTo('App\Category', 'parent_category_id');
23    }
24
25    // Recursive parents
26    public function parents() {
27        return $this->belongsTo('App\Category', 'parent_category_id')
28          			->with('parent');
29    }
30
31}