0

I'm using Laravel scout with Algolia search.

I have this eloquent relation:

public function doctors()
{
    return $this->belongsTo(Doctor::class, 'doctor_id');
}

Here I get results by Algolia search:

  $doctors = DoctorInfo::search($this->search)
                                ->with([
                                'typoTolerance' => true,
  ])
  ->paginate(10);

I get a lot of single queries:

select * from `doctors` where `doctors`.`id` = 131 limit 1
select * from `doctors` where `doctors`.`id` = 141 limit 1
select * from `doctors` where `doctors`.`id` = 191 limit 1
....

How can I get an eloquent relation using "whereIn" instead "where"?

thanks to all!

cloude
  • 338
  • 5
  • 18

1 Answers1

0

Solved in this way.

$doctors = DoctorInfo::search($this->search)
                                ->with([
                                'typoTolerance' => true,
  ])
   ->query(function ($builder) {
            $builder->with('doctors');
  })
  ->paginate(10);

My query is unique now like this:

select * from `doctors` where `doctors`.`id` in (12, 88, 107, 108, 111, 131, 168, 170, 175, 181)

Thanks for all.

cloude
  • 338
  • 5
  • 18