More of a relationship with the table below

Asked

Viewed 289 times

1

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

I searched the Eloquent documentation but couldn’t find out how to make these two relationships with the same table. For the client table and the first relationship with the Account Plan table is quiet.

A well summarized example of how are my tables

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

How to make the hasMany method inside the Model Planoconta so it works for both the id_planoconta and the id_planoconta2 of the Receipts table ?

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

  • id_planoconta, id_planoconta2 because it has these two fields, what is the reason ?

  • 1

    Ola @Virgilionovic the reason is that these two fields refer to separate accounts in the chart of accounts. In the project I am working it is necessary to classify the account to indicate what it is about (id_planoconta) and classify its receipt (id_planoconta2).

1 answer

1


The relationship in this specific case remains the same informed form in the documentation of Eloquent - 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 to say, regardless of the amount of relationships, should follow the same logic by creating a method public for each of the keys and calling in its context the responsible methods for each relationship.

Some answers already available on the site to Eloquent and Relationships:

References:

  • Any idea how to avoid 2 queries for Eager Loading planoconta1 and planoconta2? It would be very interesting to do Eager Loading of the two relationships in just 1 query... D

  • @Jhonatanraul I didn’t quite understand your doubt?

Browser other questions tagged

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