Trying to show username but it is blank.
I have an OrderResource which I use to show Orders and OrderItems. Each order Item belongs to an Order and also to a User.
In de repeater which I have in the Resource I want to show the Username who created the OrderItem (user.name)
So I have done this:
In the OrderItem Model I have created a relationship:
and in the OrderResource I have
However the user.name shows blank. How do I make this work?
21 Replies
you need to specify the name of the relationship in the
->relationship()
method
if left blank it will assume the name is 'items'
after that you don't need the 'user.name' just 'name'I tried this:
relationship('users')
But then I get this error:
Filament\Forms\Components\Repeater::getRelationship(): Return value must be of type Illuminate\Database\Eloquent\Relations\HasOneOrMany|Illuminate\Database\Eloquent\Relations\BelongsToMany|null, Illuminate\Database\Eloquent\Relations\BelongsTo returned
The OrderItem belongs to a user.you can't do that it works only on HasMany
you can try setting an invereRelationship on the OrderItem resource and see
What do you mean with inverseRelationship on OrderItem?
i think that only works for Relation Managers
in your Repeater, instead of using relationship method use Section layout
But one second in My OrderResource I also have a User Relation and there it works.
So this works in my OrderResource
But in the repeater with Items this does not work:
Both the Order and the OrderItem have a BelongsTo User relationship
have you tried something like this without the relationship?:
another approach will be to use one of the layout component in the repeater
So Removing the relationship does not work.
Adding it as a Fieldset does work but it has affect on the layout of the repeater which is not desirable
Fieldset is one of the layout component, is expected to change the layout.
you can try other layouts and see
But why does the relationship have effect on the layout?
it's not the relationship but the Fieldset component
you can try using inverseRelationship on the resource
Do you mean inverse relationship on the UserModel?
Because on the OrderItem i have a users relationship
change it to inverse
something like this:
protected static ?string $inverseRelationship = 'user';
is it 'user' or 'users'It is
user
then in your repeater:
make sure to comment out the
protected static string $relationship = 'user';
though i'm not sure it's available for Model resource, according to the docs it's only usable in a relationship manager
Have you tried using SelectInput inside the repeater?
instead of TextInput it will be SelectInput
with relationship method
I'm away for a few hours. I will check later. Thanks for your help.
my pleasure