Relate two tables through another

Asked

Viewed 309 times

0

I have 3 tables, processos, empresas and processos_empresas.

table structure:

processos:
- id 

empresas:
- id
- nome_empresa

processos_empresas:
- processo_id
- empresa_id

Brief explanation of how they relate:

A process can have multiple companies, and these process companies are saved in the table processos_empresas with the process id and the loan.

Currently within the process model there is a method called businesses that brings all companies registered in the table processos_empresas with the relationship hasMany, example:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Processo extends Model {

    function empresas () {
        return $this->hasMany('App\ProcessoEmpresas');
    }
}

What I need to do is, inside the process model, also pull table data empresa through the table processos_empresas as it is already being done, basically I also need the column company name

I found in the documentation the hasManyTrough, but I did not succeed in implementing

  • Posta a Migrations

  • Pass the model like this on the doc

  • I didn’t get it, I posted the basic structure up there

  • It’s more than that, it would be a relationship within processes > processes_companies > companies

1 answer

2

That would be a "Many-to-Many" (https://laravel.com/docs/5.7/eloquent-relationships#Many-to-Many)

Example:

<?php
namespace App;


use Illuminate\Database\Eloquent\Model;


class Processo extends Model 
{
    function empresas() 
    {
        return $this->belongsToMany(
          Empresa::class, 
          'processos_empresas', 
          'processo_id', 
          'empresa_id'
        );
    }
}
<?php

namespace App;


use Illuminate\Database\Eloquent\Model;


class Empresa extends Model 
{

    function processos() 
    {
        return $this->belongsToMany(
          Processo::class, 
          'processos_empresas', 
          'empresa_id', 
          'processo_id'
        );
    }
}

That way you’ll get:

<?php
// ...

$empresa = Empresa::with('processos')->first();
// listar todos os processos (ligados pela tabela pivot).
print_r($empresa->processos->toArray());
  • It returned a "pivot" field but only with empresa_id and processo_id, how can I return other fields? and how would it be used if I needed to return through a specific process id? Ex: Process::find(120)->companies()...

  • 1

    if you have other fields in the pivot table, you have to add in the relationship ->withPivot('campo')

  • 1

    Processo::with('empresas')->where('id', 120)->first(); or Processo::with('empresas')->whereId(120)->first(); will already return the process with companies.

  • I put withPivot in the process model and did not return no, put in the other (Company) and gave error that the column does not exist, I am doing something wrong? rs

  • In this second comment, which looks for the process by id the result is correct, without withPivot

Browser other questions tagged

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