duanlie1298 2015-01-22 08:12
浏览 31

使用雄辩的复杂联接

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:

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

+---------------------+------------------+------+-----+---------------------+----------------+
| 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. What I want is to get all related vehicles for a SirenCompany.

In raw MySQL, this is my query:

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.

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.

  • 写回答

1条回答 默认 最新

  • doudu8291 2015-01-22 14:23
    关注

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

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)