Thursday, April 13, 2017

Laravel 5.4 Eager loading belongsToMany Relationship null Binding

I have an existing model that is lazy loading a belongsToMany relationship just fine.

My problem is, when i try to eager load the relationship, i get an empty result.

When I inspect the query, it shows that the binding for the relationship query is null.

Here is my (simplified) code:

// Controller
public function filter(Request $request, App\Programs $program)
{
    $program = $program->newQuery();

    $program->select(
            'slug',
            'title',
            'season'
    );

    $program->with([
        'sports'
    ]);
    return $program->get();
}

// Model
class Programs extends Model
{
    public function sports(){
        return $this->belongsToMany('App\Sport', 'program_sport', 'program_id', 'sport_id');
    }

}



via Mdalz

Advertisement