Laravel 3 Eloquent How to select column as
Asked Answered
C

2

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!

Claud answered 4/9, 2013 at 18:30 Comment(0)
S
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'));
Suburbanize answered 28/10, 2013 at 20:54 Comment(0)
M
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();
Manganite answered 25/9, 2015 at 15:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.