php – How to join two tables with a pivot table using Laravel?-ThrowExceptions

Exception or error:

I have these three tables:

tbl_lista_contactabilidad  tbl_equipo_postventaatc  users
-------------------------  -----------------------  -----
id                         id                       id
usuarios_id                asesor_id                name

tbl_lista_contactabilidad.usuarios_id should be related with tbl_equipo_postventaatc.asesor_id. asesor_id should be the “pivot” between tbl_lista_contactabilidad.usuarios_id and users.id to make the relation.

I want to make this relation so I tried to do this relation in this way (I will put only the relation of the model)

Tbl_Lista_Contactabilidad (Model 1)

public function postventaatc(){

return $this->belongsTo('App\Models\Tbl_EquipoPostventaatc','usuarios_id');

}

Tbl_Equipo_Postventaatc (Model 2) -> This should be the pivot model

public function contactabilidad(){

return $this->hasMany('App\Models\Tbl_Lista_Contactabilidad','usuarios_id');

}

public function user(){

return $this->belongsTo('App\Models\User','asesor_id');

}

User (Model 3)

public function postventaatc(){

return $this->hasMany('App\Models\Tbl_Lista_Postventaatc','asesor_id');

}

EXAMPLE:

enter image description here

As you see in the image… if I relate usuarios_id with users directly I will get another name and I don’t want that… I want the relation just like in the image

How to solve:

A pivot table is a structure used to join two separate models together with a single relationship. This is called a many-to-many relationship in Eloquent.

From what you’ve described, this is not the case here. Rather, it looks like a has-many-through relationship.

If I’m understanding correctly, your relationships should look like this:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Tbl_Lista_Contactabilidad extends Model {
    protected $table = 'tbl_lista_contactabilidad';
    public function postventaatc() {
        return $this->belongsTo(Tbl_EquipoPostventaatc::class, 'usuarios_id');
    }
}

class Tbl_EquipoPostventaatc extends Model {
    protected $table = 'tbl_equipo_postventaatc';
    public function contactabilidad() {
        return $this->hasMany(Tbl_Lista_Contactabilidad::class, 'usuarios_id');
    }
}

class User extends Model {
    public function postventaatc() {
        return $this->belongsTo(Tbl_EquipoPostventaatc::class, 'asesor_id');
    }
    public function contactabilidad() {
        return $this->hasManyThrough(Tbl_Lista_Contactabilidad::class, Tbl_EquipoPostventaatc::class, 'asesor_id', 'usuarios_id');
    }
}

Obviously this is easier for a native English speaker, but I cannot stress how much easier this would be if you were following the Laravel rules around naming your models, tables, and columns. Why does usuarios_id column relate to a table called tbl_equipo_postventaatc? Why use asesor_id instead of user_id? 🤷🏽‍♂️ Those names have nothing to do with each other, and make it hard to figure out what is going on.

Leave a Reply

Your email address will not be published. Required fields are marked *