如何将此查询构建为laravel eloquent

时间:2017-08-16 01:19:21

标签: php mysql laravel eloquent

我有这个查询

select est.*, bf.followup as bf_follow, bf.unidad as bf_unidad, bu.unidad as bu_unidad, bu.asistencia as bu_asistencia, bu.calificacion as bu_calificacion
from estudiantes as est
inner join bluecard as bc on bc.estudiante = est.usuario_id
inner join beneficiarios as bene on bene.usuario_creado = est.usuario_id
inner join (
    select bluecard, followup, unidad
    from bluecard_followups
    order by bf_id desc
) bf on (bf.bluecard = bc.bc_id)
left join (
    select bluecard, unidad, asistencia, calificacion 
    from bluecard_unidades
    order by bu_id desc
) bu on (bu.bluecard = bc.bc_id)

where bc.status = 1
group by est.usuario_id
limit 500

我知道如何使用雄辩的联接但是自定义联接我不知道如何编写它。

有没有类似的事发生过? 有人用laravel需要这样的查询吗?

帮帮我吧! 谢谢

修改

我像这样运行查询:

$estudiantes = Estudiantes::select(
                            DB::raw("
               select est.*, bene.contrato as nro_contrato, bf.followup as bf_follow, bf.unidad as bf_unidad, bu.unidad as bu_unidad, bu.asistencia as bu_asistencia, bu.calificacion as bu_calificacion
              from estudiantes as est
              inner join bluecard as bc on bc.estudiante = est.usuario_id
              inner join beneficiarios as bene on bene.usuario_creado = est.usuario_id
              inner join (
              select bluecard, followup, unidad
              from bluecard_followups
              order by bf_id desc
              ) bf on (bf.bluecard = bc.bc_id)
              left join (
              select bluecard, unidad, asistencia, calificacion
              from bluecard_unidades
              order by bu_id desc
              ) bu on (bu.bluecard = bc.bc_id)

              where bc.status = 1
              group by est.usuario_id
              limit 500"))
                    ->get();

我收到了这个错误:

enter image description here

1 个答案:

答案 0 :(得分:0)

听起来你不需要加入而是union。 这样,您只需在单独的查询中执行当前的两个连接查询,而不是使用union函数将它们应用于另一个查询。