-1

I'm new to laravel. Here is my query that filters out the quizes that don't have any problem (questions) or the problems don't have any answers. but I'm not sure how to write it properly using laravel query builder. Considering I'm using eloquent to build relationships Would be even better if I could get the same result using laravel eloquent.

SELECT DISTINCT q.id, q.content
FROM m_quiz q
RIGHT JOIN  (SELECT DISTINCT p.quiz_id as id
             FROM  m_problem p
             RIGHT JOIN m_answer_choice a
             ON p.id = a.problem_id) problem
ON q.id = problem.id
ORDER BY q.id;
Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
Nooglers BIT
  • 71
  • 2
  • 11

2 Answers2

0

Not tested but I think it would be something like this.

 DB::table('problems')->selectRaw('m_problem.*, DISTINCT m_problem.quiz_id as id')
    ->join('m_answer_choice', 'm_problem.id', '=', 'm_answer_choice.problem_id')
    ->groupBy('m_problem.id')
    ->get();

But it would be better if you can do something like

namespace App;
use Illuminate\Database\Eloquent\Model;

    class Problem extends Model
    {
        /**
         * Get the comments for the blog post.
         */
        public function answerChoices()
        {
            return $this->hasMany('App\AnswerChoices');
        }
    }

and do something like

$answerChoices= App\Problem::find(1)->answerChoices;

foreach ($answerChoices as $answerChoice) {
    //
}
Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
0
SELECT 
    *, SUM(num_hr) AS total_hr, attendances.empID AS empid 
FROM attendances 
LEFT JOIN addsalaryemployees ON addsalaryemployees.empID=attendances.empID 
LEFT JOIN positions ON positions.id=addsalaryemployees.empPosition 
GROUP BY attendances.empID 
ORDER BY addsalaryemployees.empFirstName ASC
Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39