简体   繁体   English

使用雄辩的复杂连接

[英]Complex join using eloquent

I have three models: Vehicles: 我有三种型号:车辆:

+--------------------+------------------+------+-----+---------------------+----------------+
| Field              | Type             | Null | Key | Default             | Extra          |
+--------------------+------------------+------+-----+---------------------+----------------+
| id                 | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| registration       | varchar(255)     | NO   |     | NULL                |                |
| vin                | varchar(255)     | NO   |     | NULL                |                |
| titular_id         | int(10) unsigned | NO   |     | NULL                |                |
| titular_type       | varchar(255)     | NO   |     | NULL                |                |
| renter_id          | int(10) unsigned | NO   |     | NULL                |                |
| renter_type        | varchar(255)     | NO   |     | NULL                |                |
+--------------------+------------------+------+-----+---------------------+----------------+

SiretCompanies: SiretCompanies:

+------------------+------------------+------+-----+---------------------+----------------+
| Field            | Type             | Null | Key | Default             | Extra          |
+------------------+------------------+------+-----+---------------------+----------------+
| id               | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| siret            | varchar(255)     | NO   |     | NULL                |                |
| siren_company_id | int(10) unsigned | NO   | MUL | NULL                |                |
+------------------+------------------+------+-----+---------------------+----------------+

SirenCompany: SirenCompany:

+---------------------+------------------+------+-----+---------------------+----------------+
| Field               | Type             | Null | Key | Default             | Extra          |
+---------------------+------------------+------+-----+---------------------+----------------+
| id                  | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| siren               | varchar(255)     | NO   |     | NULL                |                |
+---------------------+------------------+------+-----+---------------------+----------------+

A vehicle can be related to the SirenCompany either through a titular, or a renter. 车辆可以通过名义或承租人与SirenCompany关联。 What I want is to get all related vehicles for a SirenCompany. 我想要的是为SirenCompany获取所有相关的车辆。

In raw MySQL, this is my query: 在原始MySQL中,这是我的查询:

select count(*) FROM `vehicles` 
inner join `siret_companies` 
    on (
        (`vehicles`.`titular_type` = 'SiretCompany' and `vehicles`.`titular_id` = `siret_companies`.`id`) 
        OR
        (`vehicles`.`renter_type` = 'SiretCompany' and `vehicles`.`renter_id` = `siret_companies`.`id`)
    )
inner join `siren_companies` 
    on `siren_companies`.`id` = `siret_companies`.`siren_company_id` 
where `siren_companies`.`id` = 410

Now what I would like to do is run that as an Eloquent query, but I cannot seem to figure it out. 现在我想做的是将其作为Eloquent查询运行,但我似乎无法弄清楚。

If I only consider the titular, I have this: 如果我只考虑标题,我有这个:

return Vehicle::join('siret_companies',function($join) {
    $join
        ->where('vehicles.titular_type', '=',  'SiretCompany')
        ->where('vehicles.titular_id','=', 'siret_companies.id');
})
->join('siren_companies', function($join) {
    $join->on('siren_companies.id', '=', 'siret_companies.siren_company_id');
})
->where('siren_companies.id','=',$this->id)
->count();

But I cannot seem to figure out how to write the join so that it corresponds to the above query. 但是我似乎无法弄清楚如何编写联接以使其与上面的查询相对应。

This is what I've come up with 这就是我想出的

Vehicle::join('siret_companies', function ($q) {

            $q->on('vehicles.titular_type', '=',  'SiretCompany');
            $q->orOn('vehicles.titular_id','=', 'siret_companies.id');
        })->join('siret_companies', function ($q) {

            $q->on('siren_companies.id', '=', 'siret_companies.siren_company_id');
        })->where('siren_companies.id','=', $id);

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

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