It's just dawned on me why the IDs are wrong. Eloquent will be using FETCH_ASSOC internally, and due to the query doing "select *" there will be collisions on the id field.
Altering the PHP as follows fixes it,
$results = TableA::with('tableB')
->join('tableABPivot', 'tableABPivot.candidate_id', '=', 'tableA.id')
->join('tableB', 'tableB.id', '=', 'tableABPivot.election_id')
->where('tableA.visible', 1)
->where('tableA.first_name', '<>', '')
->where('tableA.last_name', '<>', '')
->whereIn('tableA.affiliation_id', [100, 200])
->where('tableB.scope', 'value1')
->whereIn('tableB.rating', [4, 5, 6])
->where('tableB.type', '<>', 'value2')
->orderBy('tableA.last_name', 'ASC')
->take(100)
->get(['tableA.*']);
Note the alteration to get().
Thank you forum for being my rubber duck.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community