mercredi 11 octobre 2017

Laravel orderBy() not working with groupBy() in query builder

Im using below query to display users quiz result which is ordered by recent date.

$query = \DB::table('users_quiz_result');
    $query->join('users', 'users_quiz_result.user_id', '=', 'users.id');
    $query->join('user_profiles', 'users_quiz_result.user_id', '=', 'user_profiles.user_id');
    $query->join('quiz', 'users_quiz_result.quiz_id', '=', 'quiz.id');
    $query->select('users_quiz_result.id as quizresultid', 'users_quiz_result.invest_percent' ,'user_profiles.first_name','user_profiles.last_name', 'users.email AS email', 'quiz.name AS quizname', 'users_quiz_result.year AS quizyear',  'users_quiz_result.invest_percent AS investment','users_quiz_result.integration_percent AS integration', 'users_quiz_result.institution_percent AS institutionalisation', 'users_quiz_result.impact_percent AS impact', 'users_quiz_result.created_at AS quizdate');

    if(isset($_REQUEST['lastresult']) && trim($_REQUEST['lastresult'])==1)
    { 
        $query->groupBy('users_quiz_result.user_id');
    }
    if(isset($_REQUEST['username']) && trim($_REQUEST['username'])!='')
    {
        $query->where(function ($query) {
                $query->orwhere('user_profiles.first_name','LIKE', "%".trim($_REQUEST['username'])."%")
                      ->orwhere('user_profiles.last_name','LIKE', "%".trim($_REQUEST['username'])."%") 
                      ->orwhere(\DB::raw('CONCAT(user_profiles.first_name," ",user_profiles.last_name)'),'LIKE', "%".trim($_REQUEST['username'])."%") ; 
            });
    }

    if(isset($_REQUEST['useremail']) && trim($_REQUEST['useremail'])!='')
    {
        $query->where('users.email','LIKE', "%".trim($_REQUEST['useremail'])."%");
    }

    if(isset($_REQUEST['quizname']) && trim($_REQUEST['quizname'])!='')
    {
        $query->where('quiz.name','LIKE', "%".trim($_REQUEST['quizname'])."%");
    }

    if(isset($_REQUEST['quizname']) && trim($_REQUEST['quizname'])!='')
    {
        $query->where('quiz.name','LIKE', "%".trim($_REQUEST['quizname'])."%");
    }

    if(isset($_REQUEST['fromdate']) && trim($_REQUEST['fromdate'])!='')
    {
        $from = trim($_REQUEST['fromdate'])." 00:00:00";
        $to = trim($_REQUEST['fromdate'])." 23:59:59";

        if(isset($_REQUEST['todate']) && trim($_REQUEST['todate'])!='')
        {
            $to = trim($_REQUEST['todate'])." 23:59:59";
        }

        $query->whereBetween('users_quiz_result.created_at', [$from, $to]);
    }   

    $query->orderBy('users_quiz_result.created_at', 'DESC');
    $quizresult = $query->get();

I have search for the user records like name, email, date and also Last result of all the users. Single user can do more than one quiz.

Im using below query to display last record of all users

if(isset($_REQUEST['lastresult']) && trim($_REQUEST['lastresult'])==1)
    { 
        $query->groupBy('users_quiz_result.user_id');
    }

Using groupBy the Single user record is displaying but i cant get last record based on date.

I dont know how solve this issue please help me. Thanks in advance.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire