Pregunta

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!

¿Fue útil?

Solución

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'));

Otros consejos

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();
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top