How to create a relationship between 3 Laravel tables?

Asked

Viewed 467 times

1

I own 3 tables that it is necessary to make Join’s to access the information. By the relationship of the Laravel I can create simple relationships, like belongsTo.

I’m trying to access the information from the first table with the id of the third table. In this case, by the id of the table 'agenda_dias' I want to fetch the patient’s name.

pessoas
  -- id
  -- nome

pacientes
  -- id
  -- id_pessoa

agenda_dias
  -- id
  -- id_paciente

In Model Person I created the following function:

public function agendaDiaPaciente() 
{
    return $this->hasManyThrough(
                    'Models\AgendaDia', 'App\Models\Paciente',
                    'pessoa',           'id_paciente',         'id'
    )
}

Model of the Patient:

    public function pessoa()
{
    // Cria vinculo com tabela de pessoas. Inverso de hasOne();
    return $this->belongsTo('App\Models\Pessoa', 'id_pessoa');
}

Model da Agendadia:

public function paciente() 
{
    return $this->belongsTo('App\Models\Paciente', 'id_paciente');
}
  • 1 person can be 1 patient and one patient for having multiple day schedules ? this is the relationship? Put all models in your question?

  • A schedule_days has only 1 patient

  • so are all relationships 1 to 1 ???

  • 1

    That’s right, in this example are all related 1:1

1 answer

4


I’m just gonna worry about the relationships, they’re a little weird, but by description that would be it:

namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Pessoas extends Model
{
    public function paciente()
    {
        return $this->hasOne('App\Models\Pacientes', 'id_pessoa', 'id');
    }
}

namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Pacientes extends Model
{
    public function pessoa()
    {
        return $this->belongsTo('App\Models\Pessoas', 'id_pessoa', 'id');
    }

    public function agendadias()
    {
       return $this->hasOne('App\Models\AgendaDias', 'id_paciente', 'id');
    }
}

namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class AgendaDias extends Model
{
    public function paciente()
    {
        return $this->belongsTo('App\Models\Pacientes', 'id_paciente', 'id');
    }
}

Seeking:

$c = App\Models\AgendaDias::find(1)->paciente()->first()->pessoa()->first();

echo $c->nome;
  • Vaaaleu. I was thinking that it was not necessary to access all these relationships when defining in Model Person.

  • I had to make the following change to work. Agendadia::find(1)->patient->person->name;

  • There’s a performance difference that you did because you didn’t use it the way I did?

  • The following error appears: Badmethodcallexception in Builder.php line 2443: Call to Undefined method Illuminate Database Query Builder:person()

  • has some problem in their relations Matheus!!! because he no longer gave problem in the patient! let me observe here.

  • @Matheushahn I edited is that way SQL is optimized and the performance better... the other works, but, falls in performance...

Show 1 more comment

Browser other questions tagged

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