laravel - 如何在 Laravel 5.1 中使用 Eloquent 对原始查询进行多重选择

标签 laravel eloquent laravel-5.1

我做了一个查询,它获取一个表的所有列数据和一个别名为“距离”的附加自定义列。现在查询看起来像这样:

$restaurants = DB::table(DB::raw('restaurants'))
    ->select(
        'restaurants.id',
        'restaurants.name',
        'restaurants.about',
        'restaurants.contact_details',
        'restaurants.address',
        'restaurants.city',
        'restaurants.lat',
        'restaurants.long',
        'restaurants.cuisines',
        DB::raw(*some computation here* . " as distance")
    )
    ->get();

基本上,我的查询在 SQL 中应该如下所示:

SELECT *,*此处进行一些计算*作为与餐厅的距离

有没有办法使用 Eloquent 来简化这个?现在我需要手动指定所有列,这样我才能添加 DB::raw select 语句。

最佳答案

这应该有效:

$restaurants = DB::table('restaurants')
    ->select(
        'restaurants.*',
        DB::raw(*some computation here* . " as distance")
    )
    ->get();

关于laravel - 如何在 Laravel 5.1 中使用 Eloquent 对原始查询进行多重选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35310925/

相关文章:

php - 按不工作的公司排序。请帮助我

php - Sentry 使用什么类型的加密

php - 在 Laravel 中的关系表中添加 where 子句

php - Laravel 5.1 查询生成器 : get users with post whose body contains search queries

php - 拉维尔 5.1 : Relationships with SQLite rowid?

php - 使用 switch 后无法使用分页。 - 拉拉维尔

laravel - 每个模型都有单独的模型工厂文件

php - Laravel 5.3 对 sortBy 集合的分页

php - 删除laravel中的一行时删除所有关系

php - 如何从 Laravel 中的 N-N 关系表中的列获取值