Home > Mobile >  Laravel Query: How to convert boolean 1 and 0 into 'passed' or 'failed'
Laravel Query: How to convert boolean 1 and 0 into 'passed' or 'failed'

Time:10-11

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);
}

enter image description here

CodePudding user response:

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).

  • Related