Going deeper on Tenant relationships

I'm trying to create a resource for a child of a resource that belongs to my tenant. But since the child resource doesn't have a direct relation to the tenant, when I try to view the list page, I'm getting an error as such. I'm likely missing something obvious, and don't know if this is a Filament question or general Laravel question. The error message:
The model [App\Models\Button] does not have a relationship named [agency]. You can change the relationship being used by passing it to the [ownershipRelationship] argument of the [tenant()] method in configuration. You can change the relationship being used per-resource by setting it as the [$tenantOwnershipRelationshipName] static property on the [App\Filament\App\Resources\ButtonResource] resource class.
The model [App\Models\Button] does not have a relationship named [agency]. You can change the relationship being used by passing it to the [ownershipRelationship] argument of the [tenant()] method in configuration. You can change the relationship being used per-resource by setting it as the [$tenantOwnershipRelationshipName] static property on the [App\Filament\App\Resources\ButtonResource] resource class.
Button BelongsTo ButtonGroup ButtonGroup BelongsTo Agency Agency is my Tenant I have tried and failed to add an
agency(): BelongsTo
agency(): BelongsTo
function into my Button model, but can't get one to work.
Solution:
I found this closed Issue (https://github.com/filamentphp/filament/issues/8468), which references the
\Znck\Eloquent\Relations\BelongsToThrough
\Znck\Eloquent\Relations\BelongsToThrough
. Looked it up and installed the
staudenmeir/belongs-to-through
staudenmeir/belongs-to-through
package. I have gotten past my errors.
Jump to solution
10 Replies
MarconiMamba
MarconiMambaOP14mo ago
for clarity. I've created the resource, just when I click on it to go to the list in my panel, I get the above error message.
DrByte
DrByte14mo ago
I notice that the error message you quoted refers to a relationship named [agency] but you said you created agencies(): BelongsTo. What does the "Button" model refer to? (ie: why is it named Button?)
MarconiMamba
MarconiMambaOP14mo ago
the "Button" model is equivalent to a post in this instance. It's named Button since I'm writing this webapp as a supplemental tool for another product, and that's what the other tool calls this piece. I did make a mistake in my original post and I did create
agency(): BelongsTo
agency(): BelongsTo
updated the origial question. I'm trying something along these lines, but it isn't working:
public function agency(): BelongsTo
{
return $this->buttonGroup->agency();
}
public function agency(): BelongsTo
{
return $this->buttonGroup->agency();
}
DrByte
DrByte14mo ago
Oh. That's not how Laravel BelongsTo works. Wrong syntax.
MarconiMamba
MarconiMambaOP14mo ago
I think i have gathered that that's where I was going wrong
DrByte
DrByte14mo ago
Here's a thread where I explained a bunch about Laravel Relations including some syntax: https://discord.com/channels/883083792112300104/1160724944922103959/1160737635631902810
MarconiMamba
MarconiMambaOP14mo ago
So do I need to directly relate my Buttons to my Agencies with BelongsTo and HasMany? I can't do anything to automatically go through the middle relation? (ButtonGroup)
DrByte
DrByte14mo ago
Laravel has a HasManyThrough (and maybe BelongsToManyThrough?) relationship, which I believe Filament supports.
MarconiMamba
MarconiMambaOP14mo ago
We're getting closer. I found HasManyThrough which I put on my Agency to have many Buttons through ButtonGroups. but I do not see anything like BelongsToThrough. I tried HasOneThrough, but that's backward since my ButtonGroups doesn't have a button_id column (since it's one to many in the other direction)
Solution
MarconiMamba
MarconiMamba14mo ago
I found this closed Issue (https://github.com/filamentphp/filament/issues/8468), which references the
\Znck\Eloquent\Relations\BelongsToThrough
\Znck\Eloquent\Relations\BelongsToThrough
. Looked it up and installed the
staudenmeir/belongs-to-through
staudenmeir/belongs-to-through
package. I have gotten past my errors.
Want results from more Discord servers?
Add your server