Search
 
SCRIPT & CODE EXAMPLE
 

PHP

laravel eloquent relationships count

class Category extends Model
{

    protected $appends = [
        'articleCount'
    ];

    public function articles()
    {
        return $this->hasMany(Article::class);
    }

    public function children()
    {
        return $this->hasMany(Category::class, 'parent_id');
    }

    public function childrenArticles()
    {
        return $this->hasManyThrough(Article::class, Category::class, 'parent_id');
    }

    public function getArticleCountAttribute()
    {
        return $this->articles()->count() + $this->childrenArticles()->count();
    }
}
Comment

PREVIOUS NEXT
Code Example
Php :: array helper array_push laravel 
Php :: fetch method and class in codeigniter 
Php :: Your system folder path does not appear to be set correctly. Please open the following file and correct this: index.php 
Php :: php formData curl 
Php :: php postgresql 
Php :: php full form 
Php :: - root composer.json requires php ^7.2 but your php version (8.0.1) does not satisfy that requirement. 
Php :: laravel break 
Php :: how to log object laravel logger 
Php :: Laravel Framework upgrade from older version 7.x to 8.x 
Php :: In PackageManifest.php line 122: Undefined index: name 
Php :: filter child table data from parent laravel eloquent 
Php :: php random 
Php :: end foreach loop 
Php :: php loops 
Php :: search in laravel 8 
Php :: woocommerce checkout manager confirm password 
Php :: Diferencia entre dias PHP 
Php :: wp main menu 
Php :: root composer.json requires php ^7.3 but your php version (8.0.3) does not satisfy that requirement. 
Php :: laravel value eloquent 
Php :: laravel get age from date 
Php :: how to get attachments to emails php 
Php :: custom error page htaccess 
Php :: specification migration laravel 
Php :: wpml get site url 
Php :: set posts_per_page 
Php :: php sort multidimensional array by key 
Php :: autoload file laravel 
Php :: img upload in php 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =