1
I have a problem with Eager loading on Laravel 5.4.24. Following the documentation, I added the code:
$profiles = Profile::with(['platforms' => function ($query) {
$query->where('name', 'PC');
}])->get();
The expected return was a Collection only with Profiles who have Platform = PC. Instead I received all the records from my database, where the correct would be to receive 37 records. As shown in the query below:
SELECT COUNT(*) FROM db.profiles PROF
INNER JOIN db.platform_profile PLATPROF ON (PROF.id = PLATPROF.profile_id)
INNER JOIN db.platforms PLAT ON (PLATPROF.platform_id = PLAT.id)
WHERE PLAT.name = 'PC';
What am I missing? Could someone help me with this?
Model Profile:
class Profile extends Model
{
public function platforms()
{
return $this->belongsToMany('App\Models\Platform')->withTimestamps();
}
}
Model Platform:
class Platform extends Model
{
public function profiles()
{
return $this->belongsToMany('App\Models\Profile')->withTimestamps();
}
}
It worked as expected. Thanks for the reply!
– Ècidé