【问题标题】:Complex join using eloquent使用 eloquent 进行复杂连接
【发布时间】:2015-03-20 23:02:38
【问题描述】:

我有三个模型: 车辆:

+--------------------+------------------+------+-----+---------------------+----------------+
| 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                |                |
+--------------------+------------------+------+-----+---------------------+----------------+

Siret 公司:

+------------------+------------------+------+-----+---------------------+----------------+
| 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                |                |
+------------------+------------------+------+-----+---------------------+----------------+

警笛公司:

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

车辆可以通过名义或承租人与 SirenCompany 相关联。我想要的是获得 SirenCompany 的所有相关车辆。

在原始 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

现在我想做的是将它作为 Eloquent 查询运行,但我似乎无法弄清楚。

如果我只考虑名义上的,我有这个:

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();

但我似乎无法弄清楚如何编写连接以使其与上述查询相对应。

【问题讨论】:

    标签: php mysql laravel inner-join


    【解决方案1】:

    这是我想出来的

    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);
    

    【讨论】:

      猜你喜欢
      • 2014-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多