Laravel 3 Eloquent How to choose a column as

I am 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'; 

thanks in adv!

+7
select alias eloquent laravel laravel-3
source share
2 answers

Eloquent returns a regular Free request. So you can try something like this (assuming your model name is β€œUser”):

 $user = User::where_occupation('pimp')->get(array('occupation as test')); 
+7
source share

This is how I was able to do this in Laravel 5 using select() and pass the col name and alias to this method (here I also use groupby() to limit it to β€œ DISTINCT ” return values ​​using toarray() to return any array instead of Collection Object :

 $results = asset::select('model_code__c AS option')->whereRAW("model_code__c <> '' AND status = 'A'")->groupby('model_code__c')->get()->toarray(); 
0
source share

All Articles