List 3 tables in Laravel

Asked

Viewed 653 times

0

I have 3 tables: contratos, contratos_empresas and empresas

In the system there is a form for registration of contracts, in this form there is a select Multiple (that allows to select several companies at the same time), the <options> this select comes from the table empresas, and how several companies can be selected, the contratos does not have a field loan and this select data is saved in the table contratos_empresas saving the contrado_id and the loan.

There’s already a relationship like hasMany in the contract model, what I would like to do is to select the companies, from the table contratos_empresas, through the contract, also select companies where the id is equal to those of the contrato_empresas, it is possible to do this?

Summary for better understanding:

contrato_empresas belongs so much to contratos as empresas, and is linked to these tables via the fields: contrato_id and loan

  • 1

    You want to select only companies that have contracts. That’s it?

  • The contracts that have companies, within hasMany (contratos_empresas) of the contract, I do not know if it was very clear

3 answers

1

You must use the following format:

  1. Model: Companies, Table: Companies
  2. Model: Contract, Table: Contracts
  3. In the business model: belongsToMany, contracts()
  4. In the model contracts: belongsToMany, companies()

If you follow this template above, you can use:

Empresa::with('contratos')->get();
// Ou vice-versa: Contrato::with('empresas')->get()

This will return all companies and contracts (relationship done in the contract_companies table - pivot table) + the contract.

You will probably have to customize the pivot table (because you are using English names).

1

Talk to me, man. Next, if what you are wanting is only the data from the companies when you pull the contract data, you can create an appends (Attribute) within the contracts class, where it will pull all the data from the companies. Thus:

Contract model create property:

protected $appends = ['empresas'];

Now, within the same model create a method like this:

public function getEmpresasAttribute(){
   $empresa = ContratoEmpresas::select('empresa_id')
                                 ->where('contrado_id', '=', $this->id)
                                 ->value('empresa_id');
   return Empresas::find($empresa);
}

Warning, the function name has to have the get at the beginning and the attribute at the end, and in the middle the name of your append, always starting with uppercase.

With this in its class, the append 'companies' becomes a static property that is not in your database, and always while accessing the data of the contract class it will also show this property together.

Note: Change table and property names if they are not the same as the ones I used.

  • syntax error, Unexpected '=', expecting ';' I looked here and could not identify the missing semicolon

  • It was really a mistake of mine, but I’ve corrected the code there. Apologies.

0

This relationship we call many to many, in the documentation of the Standard shows how to deal with it with the eloquent (https://laravel.com/docs/5.7/eloquent-relationships#Many-to-Many).

In the Contract and Business model will have an Eloquent ORM method that will make this relationship for Oce and bring an array with the related elements.

In the model of Contrato would look like this:

public function empresas()
{
    return $this->belongsToMany('App\Empresa', 'contrato_empresas', 'contrato_id', 'empresa_id');
}

In the model of Empresa would look like this:

public function contratos()
{
    return $this->belongsToMany('App\Contrato', 'contrato_empresas', 'empresa_id', 'contrato_id');
}

And to access just call the method as attribute:

$empresa = Empresa::find($id);
$empresa->contratos;

$contrato = Contrato::find($id);
$contrato->empresas;

Browser other questions tagged

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