F
Filament3mo ago
Fossil

Value not updated when clicking save

I have a column called videos_id in my table. And a videos table where that ID is the primary key id and a column called title. In this dropdown on the Filament Edit page I want to show the title and not the ID. So I am doing this:
Select::make('videos_id')
->getSearchResultsUsing(fn (string $search): array => Video::where('title', 'like', "%{$search}%")->limit(50)->pluck('title', 'id')->toArray())
->getOptionLabelUsing(
function ($value) {
$video = Video::find($value);
$title = $video ? $video->title : null;
$started = $video ? $video->started : null;

$year = null;
if ($started !== null) {
$carbonDate = Carbon::createFromDate($started);
$year = $carbonDate->format('Y');
}

return $title !== null ? $title.' ('.$year.')' : null;
}
)
->searchable()
->live()
->default(0)
->label('Video ID'),
Select::make('videos_id')
->getSearchResultsUsing(fn (string $search): array => Video::where('title', 'like', "%{$search}%")->limit(50)->pluck('title', 'id')->toArray())
->getOptionLabelUsing(
function ($value) {
$video = Video::find($value);
$title = $video ? $video->title : null;
$started = $video ? $video->started : null;

$year = null;
if ($started !== null) {
$carbonDate = Carbon::createFromDate($started);
$year = $carbonDate->format('Y');
}

return $title !== null ? $title.' ('.$year.')' : null;
}
)
->searchable()
->live()
->default(0)
->label('Video ID'),
Which works fine. But when I change the value and click Save. The old ID is still present on the row. Devtools show a POST request to /livewire/update with JSON data which does contain the NEW id. So I am puzzled why this happens.
{
"data": {
"data": [
{
"id": 10,
"videos_id": "41",
{
"data": {
"data": [
{
"id": 10,
"videos_id": "41",
Removed all the other data from the JSON to make it readable.
Solution:
You should never have fields with the same name/ID. Give it a different name and use getStateUsing()
Jump to solution
3 Replies
Fossil
Fossil3mo ago
Ah I figured out why. I also have a disabled field showing the videos_id itself. It changes live when picking a new value from the dropdown but for some reason it makes the update fail. When I remove this field it works fine. Anny way to make this work?
TextInput::make('videos_id')
->numeric()
->label('Videos ID')
->disabled(),
TextInput::make('videos_id')
->numeric()
->label('Videos ID')
->disabled(),
Since it's not dehydrated I figured it would work as its not being sent to the server. In the JSON above there is only one videos_id being sent, and its the right one (the new value).
Solution
Dennis Koch
Dennis Koch3mo ago
You should never have fields with the same name/ID. Give it a different name and use getStateUsing()
Fossil
Fossil3mo ago
Cheers!