mardi 31 janvier 2017

How can I get distant many to many relationship with Eloquent

INFO: Using Laravel 5.2
BaseModel extends Eloquent

I have three models that I want to get a relationship for. Here are my models and there relationships.

class A extends BaseModel
{
    protected $table = 'a';

    use SoftDeletes;

    public function b ()
    {
            return $this->hasMany(B::class);
    }

}

For class B

class B extends BaseModel
{
    protected $table = 'b';

    use SoftDeletes;

    public function A ()
    {
            return $this->belongsTo(A::class);
    }

    public function C ()
    {
            return $this->belongsTo(C::class);
    }
}

For class C

class C extends BaseModel
{
    protected $table = 'c';

    use SoftDeletes;

    public function b ()
    {
            return $this->hasMany(B::class);
    }

}

I want to be able to get the distant C relationships from A. For instance:

$cCollection = $a->b->c;

That is find me all Cs that belong to Bs that are owned by A.

I know I can call $a->with('c.b') but that query is computationally expensive. I want to be able to call it just like a normal relationship. Any ideas?



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

Aucun commentaire:

Enregistrer un commentaire