12

I'm trying to figure out how to give a column an alias using Eloquent.

So, in other words, how do I execute the following mysql query using Eloquent?

SELECT occupation AS test FROM users WHERE occupation = 'PIMP';

Thx in adv!

Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
darksoulsong
  • 13,988
  • 14
  • 47
  • 90

2 Answers2

15

Eloquent returns a regular Fluent query. So you can try something like this (assuming your model name is 'User'):

$user = User::where_occupation('pimp')->get(array('occupation as test'));
amosmos
  • 1,039
  • 10
  • 20
2

This is how I have been able to do this in Laravel 5 using select() and passing the col name and the alias to that method (here I'm also using groupby() to restrict it to "DISTINCT" return values then using toarray() to return any array instead of a Collection Object:

$results = asset::select('model_code__c AS option')->whereRAW("model_code__c <> '' AND status = 'A'")->groupby('model_code__c')->get()->toarray();
G-Man
  • 1,138
  • 16
  • 20