Laravel Query构建器中的多对多关系

Let's consider that I have a model called Sportman and another one Sport who are linked via a pivot table : many to many relationship.

A sample code for their migrations.

# Sportmans migration
Schema::create('sportsmans', function (Blueprint $table) {
    $table->increments('id');
    $table->string('firstname');
    $table->string('lastname');
});

# Sports migration
Schema::create('sports', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('description');
});

Here is their relationship in models :

# Defining association in Sportsman model
public function sports(){
    return $this->belongsToMany( Sport::class, 'sportsman_has_sports', 'person_id', 'sport_id' );
}

# Defining association in Sports model
public function sportsman(){
    return $this->belongsToMany( Sportsman::class );
}

How can I using Laravel with Eloquent get the sportsman that play:

  1. Only "Footbal"
  2. Box or "Swimming"
  3. Both "Tennis and Basket-ball

Here is what I tried to do for question 2 :

Sportsman::with(['sports:person_id,id,name'->whereHas('sports', function ($query) {
    $query->whereIn('name', ['Box', 'Swimming'] );
});

Most difficult is the question 3

You put a subquery on whereHas function...

Sportsman::whereHas('sports', function ($query) {
    $query->whereIn('name', ['Box', 'Swimming'] );
})
->with('sports')
->get();
// Below is your last question
Sportsman::where(function ($query) {
    $query->whereHas('sports', function ($subquery) {
        $subquery->where('name', 'Tennis');
    });
    $query->whereHas('sports', function ($subquery) {
        $subquery->where('name', 'Basket-ball');
    });
})
->with('sports')
->get();