$financements = Source::select('sources.name', 'fundings.amount')
->leftJoin('fundings', 'sources.id', '=', 'fundings.source_id')
->where('fundings.training_id', 1)
->orderBy('sources.name')
->get();
Apr 6, 2021
11
Level 63
Query with multiple join : what's wrong with my query ?
Hello,
Here are my tables.
trainings : id, name
sources : id, name
fundings : training_id, source_id, amount
Here is an example of datas.
Trainings
- 1, training 1
Sources
- 1, source 1
- 2, source 2
- 3, source 3
Fundings
- 1, 1, 50
- 1, 3, 60
And here is my MySQL query.
SELECT sources.name, fundings.amount
FROM
sources
LEFT JOIN fundings ON fundings.source_id = sources.id
WHERE trainings.id = 1
ORDER BY sources.name ASC
And here is my query with Laravel.
$financements = Source::
leftJoin('fundings', 'fundings.source_id', '=', 'sources.id')
->where('fundings.training_id', 1648)
->orderBy('name')
->get();
I want to have this result.
source 1, 50
source 2, null
source 3, 60
But I have this result.
source 1, 50
source 3, 60
I work on this for a full day without having any solution.
Please help ;).
Vincent
Level 75
Ok it has to be
$financements = Source::select('sources.name', 'fundings.amount')
->leftJoin('fundings', function ($join) {
$join->on('sources.id', '=', 'fundings.source_id')->where('fundings.training_id', 1);
})
->orderBy('sources.name')
->get();
Please or to participate in this conversation.