Search
 
SCRIPT & CODE EXAMPLE
 

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());
Comment

PREVIOUS NEXT
Code Example
Php :: how to enable auto refresh on save 
Php :: how to enable autoreload on save 
Php :: create migration command in laravel 
Php :: htaccess rewrite optional parameters 
Php :: php ternary string 
Php :: laravel collection shift 
Php :: @yield laravel 
Php :: connect php mysql procedural way 
Php :: save big data with laravel 
Php :: dynamic function name php 
Php :: workpress change page title from shortcode 
Php :: while in php 
Php :: return response at failedValidation() in request laravel 
Php :: curl failed laravel centos 
Php :: php sqlite last insert id 
Php :: laravel php what does compact 
Php :: php unset by value 
Php :: php get youtube code 
Php :: what are the different types of PHP variables? 
Php :: laravel follow and unfollow relationship 
Php :: laravel get 
Php :: laravel sanctum authentication 
Php :: php distinct 
Php :: laravel excel 
Php :: how to logout in phpmyadmin 
Php :: php glue strings 
Php :: query builder codeigniter 
Php :: merge pdf php fpdf 
Php :: laravel env in js 
Php :: Undefined index: name laravel 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =