vendredi 29 avril 2016

How to get count for all products for all categories (i.e all records on many-to-many table) with laravel 5.2

This what i want to do is to take the count of entries from many-to-many table. I can do it with a simple query, but I can use the ORM of Laravel 5.2. These are my models:

Category model

class Category extends BaseModel {
    protected $table = 'categories';

    public function products() {
        return $this->belongsToMany('App\Models\Product', 'product_category');
    }

Product model

class Product extends BaseModel {
    protected $table = 'products';

    public function categories() {
        return $this->belongsToMany('App\Models\Category', 'product_category')->withTimestamps();
    }

I want to add a method in the Category model or using the Laravel ORM to take the number of all products for all categories i.e the number of all records from the product_category table. How can I do it ?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1SzDJ8o
via IFTTT

Aucun commentaire:

Enregistrer un commentaire