2

is there any way to make a ->get() using Laravel Eloquent ORM and force it to make the query using inner joins instead of using whereIn?. I have a relationship of data like this since a have a lot of data in the tables, Eloquent is taking forever to provide the data.

$people= People::with(
        'pets',
        'peopleSize',
        'cars',
        'house',
        'child'
    )->get();

This in creating a query with a whereIn statement with all the ids of the people that I have, and the same for every other relation inside of the with statment. Is there any way to change that for inner joins using Eloquent ORM?

Disa
  • 45
  • 6

2 Answers2

0

Yes it is possible you can make join in laravel. You have to read the documentation of laravel. You can use join Like this :- https://laravel.com/docs/5.4/queries#joins In your case i will provide one exmaple:-

$people= DB::table('peoples')
          ->join('pets', 'peoples.id', '=', 'pets.people_id')
          ->join('people_sizes', 'peoples.id', '=', 'people_sizes.people_id')
          ->get();

Hope it helps!

kunal
  • 4,122
  • 12
  • 40
  • 75
  • 2
    Thanks for your answer! but in that case, that would be the same if I use query builder to retrieve the data. I was wondering if i was using Eloquent ORM in the correct way. – Disa Jun 02 '17 at 16:42
0

You Can use laravel Power Join eloquent-power-joins

In your People model

use Kirschbaum\PowerJoins\PowerJoins;

use PowerJoins;
public function pets(): HasMany
{
    return $this->hasMany(Pet::class, 'people_id');
}

In Contorller

public function getData()
{
    $data = Patient::joinRelationshipUsingAlias('pets', 'p')->select('peoples.id', 'p.name')->where('peoples.id', 5)->get();
    return $data;
}