0

I have this raw query. How can i convert this one into Laravel eloquent.

SELECT * FROM `results` AS q
INNER JOIN `answers`AS a ON q.question_id = a.question_id 
AND q.team1 = a.team1 
AND q.team2 = a.team2 
AND q.score1 = a.score1

// Table relationships.
results hasMany answers.
results hasMany predictions.
Prediction is another model here.
2
  • What is the relationship between results and answers? Commented Jul 28, 2017 at 7:53
  • results hasMany answers,and answers belongs to results. @BalrajAllam Commented Jul 28, 2017 at 7:57

2 Answers 2

1

As far i understood your question,you can't get relationship with joins.

Please try this query.

 Result::join('results as q', function ($join) {
            $join->on('answers.question_id', '=', 'q.question_id')
                ->on('user_answers.team1', '=', 'q.team1')
                ->on('user_answers.team2', '=', 'q.team2')
                ->on('user_answers.score1', '=', 'q.score1')
                ->on('user_answers.score2', '=', 'q.score2');
        })->get();

Now you can run another query to get relational data.

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

Comments

0

Assume that Result is table results's model:

Result::join('answers', function ($join) {
    $join->on('answers.question_id', '=', 'results.question_id')
         ->on('answers.team1', '=', 'results.team1')
         ->on('answers.team2', '=', 'results.team2')
         ->on('answers.score1', '=', 'results.score1');
})->get();

4 Comments

Thank you for the answer,Can i also fetch relationship along with result. Like with('relation') using above query ?
What's the relationship between them? Append it to your question.
I have updated the questions,secondly i am getting "1066 - Not unique table/alias: 'answers'" while i run the above command.
I have edited my answer, may be it works. If not, add table alias as to the first parameter of join method.

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.