More than one relationship with table top

1

I have an Accounts Receivable table that has a relationship with the Customer Table and TWO relationships with a Chart of Accounts table

I searched the Eloquent documentation but did not find how I can do these two relationships with the same table. For the clients table and the first relationship with the Chart of Accounts table is quiet.

A well-summarized example of how my tables are

clientes: (id, nome, cpfcnpj)
planocontas: (id, nome, tipo)
recebimentos: (id, data, dtvco, dtpgo, valor, id_cliente, id_planoconta, id_planoconta2);

How to do the hasMany method inside the ModelContact so that it works for both the project_id and the project_id2 of the Receipts table?

I thought about creating two new tables, each for a link from the Accounts Receivable table to the Chart of Accounts table, using the Many to Many relationship.

    
asked by anonymous 17.11.2017 / 18:15

1 answer

1

The relationship in this specific case stays the same informed way in the Eloquent documentation - Laravel , example :

Relationships in your project:

class Clientes 
{
    protected $primaryKey = 'id';
    protected $table = 'clientes';
    protected $fillable = ['nome', 'cpfcnpj'];

    public function recebimentos()
    {          
        return $this->hasMany(Recebimentos::class, 'id_cliente', 'id');
    }
}
class PlanoContas
{
    protected $primaryKey = 'id';
    protected $table = 'planocontas';
    protected $fillable = ['nome', 'tipo'];

    public function recebimentos1()
    {          
        return $this->hasMany(Recebimentos::class, 'id_planoconta', 'id');
    }

    public function recebimentos2()
    {          
        return $this->hasMany(Recebimentos::class, 'id_planoconta2', 'id');
    }
}
class Recebimentos
{
    protected $primaryKey = 'id';
    protected $table = 'recebimentos';
    protected $fillable = ['data', 'dtvco','dtpgo',
                           'valor','id_cliente','id_planoconta', 
                           'id_planoconta2'];   

    public function cliente()
    {       
        return $this->belongsTo(Clientes::class, 'id_cliente', 'id');
    }

    public function planoconta1()
    {       
        return $this->belongsTo(PlanoContas::class, 'id_planoconta', 'id');
    }

    public function planoconta2()
    {       
        return $this->belongsTo(PlanoContas::class, 'id_planoconta2', 'id');
    }
}

That is, regardless of the amount of relationships , you should follow the same logic by creating a public method for each key and calling in its context the responsible methods for each relationship.

Some answers already posted on the site for Eloquent and Relationships :

17.11.2017 / 21:10