将laravel DB查询转换为Eloquent

时间:2016-07-20 06:17:07

标签: php mysql database eloquent laravel-5.2

有人可以帮我翻译这个laravel数据库查询到Eloquent吗?

SELECT
    `u`.*,
    `u`.`id` AS `userId` 
FROM
    `users` AS `u`
    INNER JOIN `user_role` AS `ur` ON `u`.`id` = `ur`.`user_id`
    INNER JOIN `user_services` AS `us` ON `u`.`id` = `us`.`user_id` 
WHERE
    `ur`.`role_id` = ? 
    AND `u`.`gender` = ? 
    AND `u`.`city` = ? 
    AND `us`.`service_id` = ?

提前谢谢

1 个答案:

答案 0 :(得分:0)

为什么要翻译成Eloquent查询?当查询很复杂时,原始SQL查询更易于阅读。

Laravel supoorts使用原始SQL:

$query = <<<EOF
  select `u`.`id` as `userId`, `u`.* from `users` as `u`
  inner join `user_role` as `ur` 
  ...
EOF;
DB::select($query, preparedArgsArray)