Force Eloquent to retrieve data using inner joins

Viewed 290

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?

2 Answers

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;
}

Related