Reputation: 70
I made a query which gets all the column data of a table and an additional custom column aliased as 'distance'. Right now the query looks like this:
$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();
Basically, my query should look like this in SQL:
SELECT *, *some computation here* as distance FROM restaurants
Is there a way to simplify this using Eloquent? Right now I need to manually specify all the columns just so I could add the DB::raw select statement.
Upvotes: 3
Views: 11637
Reputation: 1892
This should work:
$restaurants = DB::table('restaurants')
->select(
'restaurants.*',
DB::raw(*some computation here* . " as distance")
)
->get();
Upvotes: 8