Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

Laravel get all parent categories for each category

class Category extends Model {
    // Each category may have one parent
    public function parent() {
        return $this->belongsToOne(static::class, 'parent_id');
    }

    // Each category may have multiple children
    public function children() {
        return $this->hasMany(static::class, 'parent_id');
    }
}

 Category::with('children')
            ->whereNull('category_id')
            ->orderBy('name', 'asc')
            ->get());
Source by laracasts.com #
 
PREVIOUS NEXT
Tagged: #Laravel #parent #categories #category
ADD COMMENT
Topic
Name
7+9 =