Value of field ins null

In my OrderItem I have this:
Filament\Forms\Components\TextInput::make('user')
->label('Ordered By')
->disabled(),
Filament\Forms\Components\TextInput::make('user')
->label('Ordered By')
->disabled(),
The model OrderItem has this field:
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
In the database the user_id field has a value. However the above TextInput return null. How can I print the name of the related user? When I do user.name I get null
21 Replies
Bonux
Bonux4mo ago
orderItem.user.name ? or however you name your relation
LeandroFerreira
LeandroFerreira4mo ago
why not a select?
JJSanders
JJSanders4mo ago
Because it is added automatically by the model. And the field is disabled by default. It should only show who created the item
LeandroFerreira
LeandroFerreira4mo ago
the field can be ->disabled() and you can use $operation to show it only in the edit/view page
...
->disabled()
->visibleOn(['edit', 'view'])
...
->disabled()
->visibleOn(['edit', 'view'])
JJSanders
JJSanders4mo ago
Ok, but when I do:
Select::make('user.name')
->label('Ordered By')
->disabled(),
Select::make('user.name')
->label('Ordered By')
->disabled(),
I can't see the name either (in edit mode)
LeandroFerreira
LeandroFerreira4mo ago
->relationship('user', 'name')
JJSanders
JJSanders4mo ago
I had this before. But then it shows the same user in each record. Maybe something wrong in the relationship?
LeandroFerreira
LeandroFerreira4mo ago
probably
JJSanders
JJSanders4mo ago
The OrderItem belongs to a User.
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
@Leandro Ferreira Can it be it uses the User relationsship of the Order in stead of the OrderItem?
LeandroFerreira
LeandroFerreira4mo ago
yes 🤷‍♂️
JJSanders
JJSanders4mo ago
How can I rule it out ? Because in the queries I only see: "select * from users where id = 1 limit 1`"
LeandroFerreira
LeandroFerreira4mo ago
what does it mean?
JJSanders
JJSanders4mo ago
it means it selects the user where id is 1. But it doesn't rule out if it picks the relation of the user->order or order-item -> user
LeandroFerreira
LeandroFerreira4mo ago
sorry, I don't understand your question.. anyway, you should check your relationship Posts on the demo site has this relationship https://demo.filamentphp.com/blog/posts Maybe this can help you
JJSanders
JJSanders4mo ago
My main issue is the following: I have an Items Repeater for OrderItems. What I want is that when I add a new Item. The new OrderItem should be prefilled and saved with the current logged in user. When the order is saved the existing OrderItems should not be modified. The created by field of the other items should not be changed. Now In my situation all OrderITems are being related to the current user.
Omid
Omid4mo ago
Just dont pass the related columnn and have an observer fill it?
JJSanders
JJSanders4mo ago
But I need to show it on each OrderItem. I have just changed manually all user_id's in the database in the OrderItem table (for testing) now when I save the order from the UI all user_id's are overwritten and have the same user Id.
Omid
Omid4mo ago
When its null can't you get the user id via auth() ?
JJSanders
JJSanders4mo ago
Yes I can but my problem is that it updates all order items in stead of the order item added.
Omid
Omid4mo ago
Show the repeater code
JJSanders
JJSanders4mo ago
I found the error:
->mutateRelationshipDataBeforeFillUsing(function (array $data): array {
$data['user_id'] = auth()->id();

return $data;
})
->mutateRelationshipDataBeforeCreateUsing(function (array $data): array {
$data['user_id'] = auth()->id();

return $data;
})
->mutateRelationshipDataBeforeSaveUsing(function (array $data): array {
$data['user_id'] = auth()->id();

return $data;
});
->mutateRelationshipDataBeforeFillUsing(function (array $data): array {
$data['user_id'] = auth()->id();

return $data;
})
->mutateRelationshipDataBeforeCreateUsing(function (array $data): array {
$data['user_id'] = auth()->id();

return $data;
})
->mutateRelationshipDataBeforeSaveUsing(function (array $data): array {
$data['user_id'] = auth()->id();

return $data;
});
This would update all records before saving. Ouch