dimanche 25 novembre 2018

Complex relationship formation in Laravel 5

I'm working on a complex shipping cart project. I have relationships like this

CategoryGroup model

// App\CategoryGroup

class CategoryGroup extend Model 
{

    public function categories()
    {
        return $this->hasMany('App\Category');
    }
}

Category model

// App\Category

class Inventory extend Model 
{

    public function categoryGroup()
    {
        return $this->belongsTo('App\CategoryGroup');
    }


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

Product model

// App\Product

class Product extend Model 
{
    public function categories()
    {
        return $this->belongsToMany('App\Category');
    }

    public function inventories()
    {
        return $this->hasMany('App\Inventory');
    }
}

Inventory model

// App\Inventory

class Inventory extend Model 
{

    public function products()
    {
        return $this->belongsTo('App\Product');
    }
}

Now I'm stuck on the situation where I need to create a relationship between The CategoryGroups and the Inventory model like this:

// App\CategoryGroup

class CategoryGroup extend Model 
{

    public function categories()
    {
        return $this->hasMany('App\Category');
    }


    public function inventories()
    {
        // Cant figured out the way
    }

}

Is there a good way to achieve this kind of relationship?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire