【问题标题】:How to make a multiple join query with Eloquent ORM如何使用 Eloquent ORM 进行多重连接查询
【发布时间】:2021-01-17 09:56:18
【问题描述】:

长话短说,如何使用 Eloquent ORM 方法执行下一个 MySql 查询?

Ps:我想使用多重连接选项而不是多重选择或子查询,我知道 Eloquent ORM 在幕后执行,我不知道是否有可能,以提高数据库性能

select t.*,
       d.name,
       tt.name
from template t
         inner join template_type tt on t.id_template_type = tt.id_template_type
         inner join department d on t.id_department = d.id_department
         inner join user_department ud on d.id_department = ud.id_department
         inner join `user` u on ud.id_user = u.id_user
where u.id_user = 1;

模板表

| id_template | name    | id_department | id_template_type | content       |
|-------------|---------|---------------|------------------|---------------|
| 1           | temp 14 | 1             | 1                | some content  |
| 2           | temp 25 | 2             | 3                | other content |

模板类型表

| id_template_type | name       | active |
|------------------|------------|--------|
| 1                | my type    | 1      |
| 2                | elses type | 1      |

部门表

| id_department | name         | active |
|---------------|--------------|--------|
| 1             | my dept      | 1      |
| 2             | another dept | 1      |

数据透视表部门用户

| id_user_department | id_user | id_department |
|--------------------|---------|---------------|
| 1                  | 2       | 1             |
| 2                  | 6       | 2             |
| 3                  | 6       | 3             |

用户表

| id_user | name         |
|---------|--------------|
| 1       | My User      |
| 2       | Another User |

模板类

class Template extends Model
{
    protected $primaryKey = 'id_template';

    protected $table = 'template';

    public function departments()
    {
        return $this->belongsTo(Department::class);
    }

    public function types()
    {
        return $this->belongsTo(TemplateType::class);
    }
}

模板类型类

class TemplateType extends Model
{
    protected $primaryKey = 'id_template_type';

    protected $table = 'template_type';

    public function templates()
    {
        $this->hasMany(Template::class, 'id_template_type', 'id_template_type');
    }
}

部门类

class Department extends Model
{
    protected $table = 'department';

    protected $primaryKey = 'id_department';

    public function users()
    {
        return $this->belongsToMany(Users::class, 'user_department', 'id_department', 'id_user');
    }
}

用户类

class User extends Model
{
    protected $table = 'user';

    protected $primaryKey = 'id_user';

    public function departments()
    {
        return $this->belongsToMany(Department::class, 'user_department', 'id_user', 'id_department');
    }
}

【问题讨论】:

    标签: php mysql eloquent orm eloquent-relationship


    【解决方案1】:

    如果你正确地建立了你们的关系。你可以这样做:

    $templates = Template::with('type', 'department')
                         ->whereHas('department', function($query) use ($user_id) {
                             return $query->whereHas('users', function($user_query) use ($user_id) {
                                 return $user_query->where('id', $id')
                             });
                         })
    

    从部门到用户应该是多对多here

    【讨论】:

    • 我已经用我制作的模型和关系更新了问题,也许我做错了什么,提前谢谢。
    • 首先,你应该将typesdepartments从模板更改为typedepartment,因为代码约定,一个模板属于一个类型,部门。
    猜你喜欢
    • 1970-01-01
    • 2023-04-01
    • 2022-01-05
    • 2015-08-13
    • 1970-01-01
    • 2017-07-17
    • 2021-08-30
    • 1970-01-01
    • 2017-05-05
    相关资源
    最近更新 更多