简体   繁体   English

如何使用 Laravel 将两个表与数据透视表连接起来?

[英]How to join two tables with a pivot table using Laravel?

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 . tbl_lista_contactabilidad.usuarios_id应该与tbl_equipo_postventaatc.asesor_id相关。 asesor_id should be the "pivot" between tbl_lista_contactabilidad.usuarios_id and users.id to make the relation. asesor_id应该是tbl_lista_contactabilidad.usuarios_idusers.id之间的“枢轴”以建立关系。

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) Tbl_Lista_Contactabilidad(模型 1)

public function postventaatc(){

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

}

Tbl_Equipo_Postventaatc (Model 2) -> This should be the pivot model Tbl_Equipo_Postventaatc (Model 2) -> 这应该是枢轴模型

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)用户(模型 3)

public function postventaatc(){

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

}

EXAMPLE:例子:

在此处输入图片说明

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正如你在图片中看到的......如果我直接将 usuarios_id 与用户相关联,我会得到另一个名字,我不想要那个......我想要像图片中那样的关系

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.这在 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.显然,这对于以英语为母语的人来说更容易,但我不能强调如果您遵循 Laravel 规则来命名模型、表格和列,这会容易得多。 Why does usuarios_id column relate to a table called tbl_equipo_postventaatc ?为什么usuarios_id列涉及称为表tbl_equipo_postventaatc Why use asesor_id instead of user_id ?为什么使用asesor_id而不是user_id 🤷🏽‍♂️ Those names have nothing to do with each other, and make it hard to figure out what is going on. 🤷🏽‍♂️ 那些名字彼此没有任何关系,让人很难弄清楚发生了什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM