Skip to content

Model::automaticallyEagerLoadRelationships doesn't work for BelongsToMany #55438

Closed
@decadence

Description

@decadence

Laravel Version

12.9.0

PHP Version

8.3.8

Database Driver & Version

MySQL 8.4

Description

Global Model::automaticallyEagerLoadRelationships doesn't work for BelongsToMany because it has its own get method which does not take into account this functionality.

And if you use Model::preventLazyLoading you get LazyLoadingViolationException.

Steps To Reproduce

Model::automaticallyEagerLoadRelationships();
Model::preventLazyLoading();

// contracts is BelongsToMany relation
$contracts = $project->contracts()->get();

foreach ($contracts as $contract) {
    // LazyLoadingViolationException here
    $contractors[] = $contract->contractor;
}

@litvinchuk

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions