Issue
I need to set this query in Laravel and I believe that DB:Raw is the best alterntive, how can I set this query in Eloquent or as DB:raw?
SELECT PROP_TYPE,
concat(round(avg( prop_exclusive = 'Exclusiva' ) * 100, 0),'%') as Exclusiva,
concat(round(avg( prop_exclusive = 'No Exclusiva' ) * 100, 0),'%') as No_Exclusiva
FROM PROPERTIES
WHERE PROP_MARKET_CENTER = 'KW' AND
PROP_EXCLUSIVE IS NOT NULL
GROUP BY PROP_TYPE
ORDER BY PROP_TYPE ASC
Regards!
Solution
I presume yo have a Property.php
model similar to this.
class Property extends Model
{
$table = 'PROPERTIES';
}
From here just use DB::raw()
for the selects and the rest is standard usage of the Eloquent QueryBuilder.
$properties = Property::select(
'PROP_TYPE',
DB::raw("concat(round(avg( prop_exclusive = 'Exclusiva' ) * 100, 0),'%') as Exclusiva"),
DB::raw("concat(round(avg( prop_exclusive = 'No Exclusiva' ) * 100, 0),'%') as No_Exclusiva")
)
->where('PROP_MARKET_CENTER', 'KW')
->whereNotNull('PROP_EXCLUSIVE')
->groupBy('PROP_TYPE')
->orderBy('PROP_TYPE', 'asc');
Answered By - mrhn
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.