0

I need help to build a Laravel query from my raw SQL Query. I tried many way and did not find my Luck. Can anybody help me? My Raw SQL code is given bellow.

SELECT exams. * , count( question_details.exam_id ) AS qus_enter
FROM exams
INNER JOIN question_details ON exams.id = question_details.exam_id GROUP BY exams.id

This is what I've tried:

$examListsID = DB::table('exams')
                 ->join('question_details', function($join) {
                     $join->on('exams.id', '=', 'question_details.exam_id as qus_enter');
                 })
                 ->whereraw('count(qus_enter) = exams.total_question')
                 ->select('exams.id as examID','qus_enter','exams.total_question')
                 ->count('qus_enter')
                 ->groupby('exams.id')
                 ->get();

$examLists = Addexam::where('id','=',$examListsID->examID)

And I Get this Error:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as qus_enter where count(qus_enter) = exams.total_question' at line 1 (SQL: select count(qus_enter) as aggregate from exams inner join question_details on exams.id = question_details.exam_id as qus_enter where count(qus_enter) = exams.total_question)

6
  • Then please show us what you actually tried Commented May 4, 2015 at 7:21
  • I have two table one is 'exams' and 'question_details'. in my 'exams' table i set the total question of that exam and in my 'question_details' I entered the questions with 'exam_id'. I want to load only that exam which have all question inserted in 'question_details' table. Commented May 4, 2015 at 7:30
  • I mean I want you to update your answer with the Laravel query code (even if it's not working) Commented May 4, 2015 at 7:31
  • $examListsID = DB::table('exams') ->join('question_details', function($join) { $join->on('exams.id', '=', 'question_details.exam_id as qus_enter'); }) ->whereraw('count(qus_enter) = exams.total_question') ->select('exams.id as examID','qus_enter','exams.total_question') ->count('qus_enter') ->groupby('exams.id') ->get(); $examLists = Addexam::where('id','=',$examListsID->examID); Commented May 4, 2015 at 7:33
  • For the next time, please edit your question to add details. I've done that now for you... Commented May 4, 2015 at 7:36

2 Answers 2

1
$result = DB::table('exams')->join('question_details','exams.id','=','question_details.exam_id')->select([
  exams.*,
  DB::raw('count( question_details.exam_id ) AS qus_enter')
])->GroupBy('exams.id')->get()

Hope this helps

Sign up to request clarification or add additional context in comments.

Comments

0
DB::listen(function ($data) { 
var_dump($data->bindings);
    dd($data->sql);
});

1 Comment

Welcome. Can you offer an explanation of what you are doing here? That will help future readers with similar problems, but who may be unfamiliar with this syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.