Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.