Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 3 Eloquent How to select column as

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!

like image 678
darksoulsong Avatar asked Sep 04 '13 18:09

darksoulsong


Video Answer


2 Answers

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();
like image 68
G-Man Avatar answered Oct 23 '22 11:10

G-Man


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'));
like image 37
amosmos Avatar answered Oct 23 '22 12:10

amosmos