I currently have a query where it shows the Quiz and the users who have taken it, including the total points of the quiz, name, number of correct answers and lastly the ‘status’ which is the 1 or 0 or the passed or failed.
I want to show in the status if it is ‘passed’ or ‘failed’ instead of 1 or 0.
public function getQuizInfoByQuizID(Request $request, $id){ $remarks = DB::table('user_scores')->where('quiz_id',$id) ->Join('quiz_information','quiz_information.id', '=', 'quiz_id') ->rightJoin('users','users.id', '=', 'user_id') ->select('quiz_information.quiz_title AS Quiz Title','total_points AS Points','number_of_correct_answers', 'users.name AS Name','remarks as STATUS') ->groupBy('quiz_title','total_points','number_of_correct_answers','name','remarks') ->get() ->toArray(); return response(['message'=>"Remarks successfuly shown", 'error'=>false, 'error code'=>200, 'line'=>"line".__LINE__."".basename(__LINE__), 'quizRemarks'=>$remarks],200,[],JSON_NUMERIC_CHECK); }
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You can use if
statement in select
query like if(remarks=1,"passed","faild")
.
So replace this instead of select
:
...->select('quiz_information.quiz_title AS Quiz Title','total_points AS Points','number_of_correct_answers',
'users.name AS Name',DB::raw('if(remarks=1,"passed","faild") as STATUS'))
But you can use the models (Eloquent), then use accessor (Eloquent accessor).
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0