How to do a Left Outer join with Laravel?

Dave Driesmans

i want information from one table and if there is matched info from another table that as well.

this my code

 $scoreObject = DB::table('responses')
        ->select('responses.id', 'responses.questions_id', 'responses.answer_id', 'responses.open_answer', 'responses.user_id',  'responses.scan_id',
             'questions.question', 'questions.question_nr', 'questions.type', 'questions.totalsection_id',
            'answers.id as answerID', 'answers.answer', 'answers.questions_id', 'answers.points'
        )
        ->Join('answers as answers', 'responses.answer_id', '=', 'answers.id')
        ->Join('questions as questions', 'answers.questions_id', '=', 'questions.id')
        ->orderBy('questions.id', 'ASC')
        ->where('responses.scan_id', $scanid)
        ->where('responses.user_id', $userid)
        ->groupBy('questions.id')
        ->get();

It returns all responses that have matches with answers (answers.questions_id questions.id'). some responses don't have matched (because there is no responses.answer_id) but i still want the responses info then.

how can i get such a left outer join in laravel ?

Bogdan

You could try specifying the join as being a left outer join:

->join('answers as answers', 'responses.answer_id', '=', 'answers.id', 'left outer')

The fourth parameter of the join method is $type, which when not specified, defaults to the value inner. But since left join and left outer join are the same thing, you could just use the leftJoin method instead, to make it more readable:

->leftJoin('answers as answers', 'responses.answer_id', '=', 'answers.id')

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to execute "left outer join" in SqlAlchemy

From Dev

MySQL - how do I do an outer join where either side is optional - left and right outer join

From Dev

how to write combination of RIGHT OUTER JOIN and LEFT OUTER JOIN

From Dev

LEFT OUTER JOIN with LIMIT

From Dev

Outer apply and left join

From Dev

SQL JOIN and LEFT OUTER JOIN

From Dev

Combined Left Outer Join and Full Outer Join

From Dev

How to combine inner join and left outer join in Rails

From Dev

Left Outer Join SOQL

From Dev

mysql - how to UPDATE after LEFT OUTER JOIN

From Dev

Django Left Outer Join

From Dev

How to implement left outer join in python pandas?

From Dev

How can I get a LEFT JOIN or FULL OUTER JOIN?

From Dev

How to do a left outer join in Entity Framework without using the query syntax?

From Dev

How to do left outer join exclusion in pandas

From Dev

How to do LEFT OUTER JOIN with include

From Dev

Laravel 8 - Left Outer Join with multiple conditions

From Dev

how to make a left outer join in following scenario

From Dev

LEFT OUTER JOIN problems

From Dev

MySQL Query - How do I get a SUM with GROUP BY and WHERE condition and use LEFT OUTER JOIN?

From Dev

Right Outer Join to Left Outer join

From Dev

Left Outer Join Issue

From Dev

How to implement left outer join?

From Dev

How to use left outer join in Linq

From Dev

LEFT (OUTER) JOIN

From Dev

How to Write Left Outer join for this SQL query?

From Dev

How to do a double left outer join in Linq query syntax(or fluent)

From Dev

MySQl problems, trying to do a left outer join with three tables

From Dev

how can i convert this to left outer join?