Convert this query for eloquent(将此查询转换为 eloquent)
问题描述
我正在处理一个 Laravel 项目并尝试将此查询转换为雄辩的查询,以便我可以急切地加载其他关系.
I'm working on a Laravel project and trying to convert this query into an eloquent query so I can eager load other relationships.
$restaurants = DB::query('SELECT *, ( 3959 * acos( cos( radians(21.420639) ) * cos( radians( lat ) ) * cos( radians( lon ) - radians(-157.805745) ) + sin( radians(21.420639) ) * sin( radians( lat ) ) ) ) AS distance FROM restaurants GROUP BY id HAVING distance < 25 AND ratingsTotal > 0 ORDER BY distance LIMIT 0 , 5');
任何有关其外观的帮助将不胜感激.
Any help on how this should look would really be appreciated.
推荐答案
你的模型将在 Laravel 4 中看起来像这样.L3 可能非常相似,但我几乎没有经验.
Your model is going to look something like this in Laravel 4. L3 is probably fairly similar but I have little-to-no experience with that.
<?php
class Restaurant extends IlluminateDatabaseEloquentModel
{
protected $table = 'restaurants';
public function getSomeShit()
{
return $this
->select('*')
->select(DB::raw('( 3959 * acos( cos( radians(21.420639) ) * cos( radians( lat ) ) * cos( radians( lon ) - radians(-157.805745) ) + sin( radians(21.420639) ) * sin( radians( lat ) ) ) ) AS distance'))
->groupBy('id')
->having('distance', '<', 25)
->having('ratingsTotal', '>', 0)
->orderBy('distance')
->limit(5);
}
}
请记住,您永远不要在控制器中放置复杂(或任何)数据库逻辑,而要始终放在视图后面.
Remember, that you never put complicated (or any) database logic in the controller, always behind a view.
这篇关于将此查询转换为 eloquent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将此查询转换为 eloquent
基础教程推荐
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01