how to eager load in relationship manager

i have these models
class Company extends Model
{
    public function events(): BelongsToMany
    {
        return $this->belongsToMany(Event::class)
            ->using(CompanyEvent::class)
            ->as('events')
            ->withPivot([
                'profile_id'
            ]);
    }

    public function profiles(): HasMany
    {
        return $this->hasMany(CompanyProfile::class);
    }
}

class Event extends Model
{
    public function companies(): BelongsToMany
    {
        return $this->belongsToMany(Company::class)
            ->as('companies')
            ->using(CompanyEvent::class)
            ->withPivot([
                'profile_id',
            ]);
    }
}

class CompanyEvent extends Pivot
{
    protected $fillable = [
        'company_id', 'event_id', 'profile_id',
    ];

    public function profile(): BelongsTo
    {
        return $this->belongsTo(CompanyProfile::class);
    }
}


the company_event table is unique by 3 columns
now in my EventResource->CompaniesRelationManager, I would want to display the profile name, i tried Tables\Columns\TextColumn::make('companies.profile.name'), while it works it is having N+1
image.png
Was this page helpful?