Search code examples
phplaravel-5.3

Laravel Custom Query


I have this query

SELECT ANY_VALUE(id) as id, title FROM `major` where university_id=1 group BY `title` order by id asc

I want to convert it into Laravel Query , I have a model majors and the function as follow

public static function retrieveByUniversityIDForWeb($universityId){
    return self::select(DB::raw('ANY_VALUE(id) as id, title'))->from('major')->where('university_id', $universityId)->orderBy('id','desc')->simplePaginate(6);
}

but its not returning me the results , query works in phpmyadmin. Any idea what I missed?


Solution

  • You're declaring the method on your model which references its table by default. Also there's no need for using ANY_VALUE in your query. If you need it for some reason then you can change the select below to selectRaw('ANY_VALUE(id) as id, title')

    public static function retrieveByUniversityIDForWeb($universityId)
    {
        return self::select('id', 'title')
            ->where('university_id', $universityId)
            ->orderBy('id', 'desc')
            ->simplePaginate(6);
    }