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

Che*_*una 2 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();
Run Code Online (Sandbox Code Playgroud)

基本上,我的查询在 SQL 中应该是这样的:

SELECT *, *some computation here* as distance FROM restaurants

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

Roj*_*men 5

这应该有效:

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