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