Skip to content

fix: skip tenant association for HasOneThrough ownership relationships - #20506

Merged
danharrin merged 2 commits into
filamentphp:4.xfrom
sawirricardo:fix/tenancy-has-one-through-ownership
Sep 15, 2026
Merged

danharrin merged 2 commits into
filamentphp:4.xfrom
sawirricardo:fix/tenancy-has-one-through-ownership

Conversation

@sawirricardo

Copy link
Copy Markdown
Contributor

Description

Fixes #20505.

BelongsToTenant::observeTenancyModelCreation() registers a created observer that returns early for BelongsTo and BelongsToThrough ownership relationships, syncs BelongsToMany, and otherwise calls $relationship->save($tenant). Laravel's HasOneThrough / HasManyThrough are read-only and have no save(), so a resource whose ownership relationship goes through an intermediate model (for example post -> author -> team) crashes on create with:

BadMethodCallException: Call to undefined method Illuminate\Database\Eloquent\Relations\HasOneThrough::save()

This is the tenancy counterpart of #15264 / #15265 (v3), which switched the other relationship checks to the HasOneOrManyThrough base class.

The change adds HasOneOrManyThrough to the early-return guard. The tenant is already implied by the intermediate model, so nothing needs to be written. Query scoping is unaffected because scopeEloquentQueryToTenant() already handles this relationship via whereHas().

Tests:

  • tests/src/Panels/Tenancy/HasOneThroughOwnershipTest.php adds a Post::teamThroughAuthor(): HasOneThrough fixture relationship and a resource that uses it for ownership. The first test fails on 4.x with the exception above and passes with this change; the second confirms tenant scoping still filters records through the same relationship.
  • Existing tests/src/Panels/Tenancy and tests/src/Panels/Pages/Tenancy suites pass (12 tests).
  • phpstan is clean on the trait; composer cs (rector + pint) produced no changes.

The same code exists on 5.x and 6.x.

Visual changes

None.

Functional changes

  • Code style has been fixed by running the composer cs command.
  • Changes have been tested to not break existing functionality.
  • Documentation is up-to-date.

The `created` observer registered by `observeTenancyModelCreation()` only
returned early for `BelongsTo` and `BelongsToThrough` ownership
relationships before calling `$relationship->save($tenant)`. Laravel's
`HasOneThrough` and `HasManyThrough` are read-only and have no `save()`
method, so creating a record in a resource whose ownership relationship
goes through an intermediate model crashed with
`Call to undefined method HasOneThrough::save()`.

Treat `HasOneOrManyThrough` like the other non-writable ownership
relationships and return early, since the tenant is already implied by
the intermediate model. Query scoping through `whereHas()` is unaffected.
@danharrin danharrin added bug Something isn't working pending review labels Sep 15, 2026
@danharrin danharrin added this to the v4 milestone Sep 15, 2026
@danharrin
danharrin merged commit 6c59628 into filamentphp:4.x Sep 15, 2026
@sawirricardo
sawirricardo deleted the fix/tenancy-has-one-through-ownership branch September 15, 2026 14:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tenancy: creating a record crashes when the ownership relationship is a HasOneThrough (HasOneThrough::save() is undefined)

2 participants