将此查询转换为 eloquent

Convert this query for eloquent(将此查询转换为 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)