Repository navigation
Conversation
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
approved these changes
Sep 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #20505.
BelongsToTenant::observeTenancyModelCreation()registers acreatedobserver that returns early forBelongsToandBelongsToThroughownership relationships, syncsBelongsToMany, and otherwise calls$relationship->save($tenant). Laravel'sHasOneThrough/HasManyThroughare read-only and have nosave(), so a resource whose ownership relationship goes through an intermediate model (for examplepost -> author -> team) crashes on create with:This is the tenancy counterpart of #15264 / #15265 (v3), which switched the other relationship checks to the
HasOneOrManyThroughbase class.The change adds
HasOneOrManyThroughto the early-return guard. The tenant is already implied by the intermediate model, so nothing needs to be written. Query scoping is unaffected becausescopeEloquentQueryToTenant()already handles this relationship viawhereHas().Tests:
tests/src/Panels/Tenancy/HasOneThroughOwnershipTest.phpadds aPost::teamThroughAuthor(): HasOneThroughfixture relationship and a resource that uses it for ownership. The first test fails on4.xwith the exception above and passes with this change; the second confirms tenant scoping still filters records through the same relationship.tests/src/Panels/Tenancyandtests/src/Panels/Pages/Tenancysuites pass (12 tests).phpstanis clean on the trait;composer cs(rector + pint) produced no changes.The same code exists on
5.xand6.x.Visual changes
None.
Functional changes
composer cscommand.