But this results in 2 queries instead of one, and in my original mysql query it searches for at least 2 same genres occurrence.
That's how eager loading, et al.. works with eloquent. It doesn't use joins.
Given the relationships:
(Movie Model):
public function genres() {
return $this->belongsToMany(App\Genre::class);
}
(Genre Model):
public function movies() {
return $this->belongsToMany(App\Movie::class);
}
You'd eager load thusly:
// Load all movies with their genre(s):
$movies = Movie::with('genres')->get();
// Load all genres with their movie(s):
$genres = Genre::with('movies')->get();
Each of those would run exactly two queries.
NOTE: Since you're using different naming conventions than eloquent expects, you'll need to specify table and key names on those relationships.