As the title says, for some reason when using DB::raw and an alias within DB::raw the alias name (for example Score) is not recognized by the OrderBy method in the query builder. Below is how I am trying to get the OrderBy to work.
Any help on this matter would be great.
DB::table('applications')
...
->select(..., DB::raw('CONCAT(student.lastName, ", ", student.firstName) AS name'), DB::raw('ROUND (AVG(applicationAssessment.total), 2) + studentDemographics.GPA AS Score, ...
)
->where('aidyears.status', '=', 1)
->whereIn('applications.statusID', array(5,8))
->GroupBy('applicationAssessment.applicationID')
->OrderBy('Score', 'desc');
Also I am having issue even using the alias without the DB::raw.
Like this in this example:
->select('applicationAssessment.applicationID AS appID',...........)..........
->groupBy('appID')
I still get the Column appID (in this case) not found?
select
`applicationAssessment`.`applicationID` as `appID`,
CONCAT(student.lastName, ", ", student.firstName) as name,
ROUND (AVG(applicationAssessment.total), 2) + studentDemographics.GPA AS Score,
..............(other non relevant stuff here like joins and what not)
group by `appID`"
I copy/paste word for word the entire query (I'm not showing it all for readability purposes) and it works in SequalPro (OSX) but not in laravel 4
Convert your laravel query to plain text with toSql() and execute with laravel DB::select ,worked for me.
$result_query = $query->toSql(); $result = DB::select(DB::raw($result_query));
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community