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:   
 public function user(): BelongsTo
 {
     return $this->belongsTo(User::class);
 }
 public function user(): BelongsTo
 {
     return $this->belongsTo(User::class);
 }
and in the OrderResource I have
return Repeater::make('items')
->relationship()
  ->schema([
TextInput::make('user.name')
     ->label('Ordered by')
->disabled(),
]);
return Repeater::make('items')
->relationship()
  ->schema([
TextInput::make('user.name')
     ->label('Ordered by')
->disabled(),
]);
However the user.name shows blank. How do I make this work?
21 Replies
Hussain4real
Hussain4real12mo ago
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'
JJSanders
JJSandersOP12mo ago
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.
Hussain4real
Hussain4real12mo ago
you can't do that it works only on HasMany you can try setting an invereRelationship on the OrderItem resource and see
JJSanders
JJSandersOP12mo ago
What do you mean with inverseRelationship on OrderItem?
Hussain4real
Hussain4real12mo ago
i think that only works for Relation Managers in your Repeater, instead of using relationship method use Section layout
JJSanders
JJSandersOP12mo ago
But one second in My OrderResource I also have a User Relation and there it works. So this works in my OrderResource
Tables\Columns\TextColumn::make('user.name')
->label('Created By')
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('user.name')
->label('Created By')
->searchable()
->sortable(),
But in the repeater with Items this does not work:
return Repeater::make('items')
->relationship()
->schema([
TextInput::make('user.name')
->label('Ordered by')
->disabled(),
]);
return Repeater::make('items')
->relationship()
->schema([
TextInput::make('user.name')
->label('Ordered by')
->disabled(),
]);
Both the Order and the OrderItem have a BelongsTo User relationship
Hussain4real
Hussain4real12mo ago
have you tried something like this without the relationship?:
Repeater::make('items')
->schema([
TextInput::make('user.name')
->label('Ordered by')
->disabled(),
]);
Repeater::make('items')
->schema([
TextInput::make('user.name')
->label('Ordered by')
->disabled(),
]);
another approach will be to use one of the layout component in the repeater
return Repeater::make('items')
->schema([
Fieldset::make('user')
->relationship('user')
->schema([
TextInput::make('user.name')
->label('Ordered by')
->disabled(),
])
]);
return Repeater::make('items')
->schema([
Fieldset::make('user')
->relationship('user')
->schema([
TextInput::make('user.name')
->label('Ordered by')
->disabled(),
])
]);
JJSanders
JJSandersOP12mo ago
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
No description
Hussain4real
Hussain4real12mo ago
Fieldset is one of the layout component, is expected to change the layout. you can try other layouts and see
JJSanders
JJSandersOP12mo ago
But why does the relationship have effect on the layout?
Hussain4real
Hussain4real12mo ago
it's not the relationship but the Fieldset component you can try using inverseRelationship on the resource
JJSanders
JJSandersOP12mo ago
Do you mean inverse relationship on the UserModel? Because on the OrderItem i have a users relationship
Hussain4real
Hussain4real12mo ago
change it to inverse something like this: protected static ?string $inverseRelationship = 'user'; is it 'user' or 'users'
JJSanders
JJSandersOP12mo ago
It is user
Hussain4real
Hussain4real12mo ago
then in your repeater:
return Repeater::make('items')
->relationship('user)
->schema([
TextInput::make('user.name')
->label('Ordered by')
->disabled(),
]);
return Repeater::make('items')
->relationship('user)
->schema([
TextInput::make('user.name')
->label('Ordered by')
->disabled(),
]);
make sure to comment out the protected static string $relationship = 'user';
JJSanders
JJSandersOP12mo ago
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
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
Hussain4real
Hussain4real12mo ago
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
JJSanders
JJSandersOP12mo ago
I'm away for a few hours. I will check later. Thanks for your help.
Hussain4real
Hussain4real12mo ago
my pleasure
Want results from more Discord servers?
Add your server