Skip to content

Using ->chaperone on model with global automaticallyEagerLoadRelationships causes infinite loop #55526

Closed
@Razunter

Description

@Razunter

Laravel Version

12.10.1

PHP Version

8.3

Database Driver & Version

No response

Description

Using ->chaperone() on a relationship of a model with globally enabled automaticallyEagerLoadRelationships causes infinite loop in HasRelationships.php

    protected function invokeRelationAutoloadCallbackFor($key, $tuples)
    {
        $tuples = array_merge([[$key, get_class($this)]], $tuples);

        call_user_func($this->relationAutoloadCallback, $tuples);
    }

Steps To Reproduce

Repo: https://github.com/Razunter/laravel-chaperone-bug

class Product extends Model
{
    public function analogsGroup(): HasOne
    {
        return $this->hasOne(ProductsAnalogsGroup::class, 'product_id', 'id');
    }

    public function variants(): HasMany
    {
        return $this->hasMany(ProductVariant::class)
          ->chaperone();
    }
}
class ProductVariant extends Model
{
    public function product(): BelongsTo
    {
        return $this->belongsTo(Product::class);
    }
}

calling \App\Models\Product::with('variants')->first()->analogsGroup causes infinite loop between Product and ProductVariant.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions