Voglio informazioni da una tabella e se ci sono anche le informazioni corrispondenti da un'altra tabella.Come fare un join esterno sinistro con Laravel?
questo mio codice
$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();
Si restituisce tutte le risposte che hanno corrispondenze con le risposte (answers.questions_id questions.id '). alcune risposte non corrispondono (perché non c'è responses.answer_id) ma voglio comunque le informazioni sulle risposte.
Come posso ottenere un tale membro esterno sinistro in laravel?
Si potrebbe provare a specificare il join ad essere un esterno sinistro ** join **: '-> join ('risposte come risposte', 'responses.answer_id', '=', 'answers.id', 'left outer')'. L'ultimo parametro (facoltativo) del metodo 'join' è' $ tipo', che, quando non specificato, imposta automaticamente il valore 'inner'. – Bogdan
@Bogdan Questo dovrebbe funzionare. Scrivilo come risposta;) – lukasgeiter
@Bogdan, in effetti funziona! se lo metti come risposta, posso contrassegnarlo come corretto. strano che non sia meglio documentato qui http://laravel.com/docs/4.2/queries#joins. –