Use of the Eloquent hasManyThrough

Asked

Viewed 184 times

0

    // Modelo Cliente
    public function plano()
    {
    return $this->hasManyThrough('App\Plano', 'App\Plano_cliente','cliente_id','id','plano_id');
     }

    //Modelo Plano
    public function cliente()
    {
    return $this->hasManyThrough('App\Cliente', 'App\Plano_cliente','plano_id','id','cliente_id');
     }
    // Modelo Plano_cliente
    protected function cliente(){
    return $this->belongsToMany('App\Cliente')->withPivot('cliente_id', 'id');
    }
        protected function plano(){
    return $this->belongsToMany('App\Plano')->withPivot('plano_id', 'id');
    }

These are the three models Client -> plano_client <- Plan

When I do the consultation: Client::with(plan)->get();

returns in the resultset all clients but only the foreground of each Each client can have several plans

1 answer

1

Modelo Cliente

    public function plano()
    {
    return $this->belongsToMany('App\Plano', 'plano_cliente','cliente_id','plano_id');
    }

Flat model

     public function cliente()
    {
    return $this->belongsToMany('App\Cliente', 'plano_cliente','plano_id','cliente_id');
    }

Model plano_client

    protected function cliente(){
    return $this->belongsToMany('App\Cliente');
    }
    protected function plano(){
    return $this->belongsToMany('App\Plano');
    }

So it all worked out

  • Exact and remember that can user a ->has('plan') or a ->with('plan'), being the first will be in the object what has a plan and in the second all with the plans.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.