Simple repeater values not showing on edit

I'm sure I'm doing something wrong but I can't figure out what. I have a simple repeater field for a JSON column called times (just a time value eg. 8:00 AM) . The Model casts this field as an array. I can create records and display the value of the times column as a badge on my table, no problem there. But when I click edit. The repeater shows all the imputs but with no values in them My repeater column definition
Forms\Components\Repeater::make('times')
->simple(
Forms\Components\TimePicker::make('medication_time')
->required()
->native(false)
->seconds(false)
->displayFormat('h:i A')
)
Forms\Components\Repeater::make('times')
->simple(
Forms\Components\TimePicker::make('medication_time')
->required()
->native(false)
->seconds(false)
->displayFormat('h:i A')
)
I appreciate all the help I can get.
No description
No description
Solution:
More specifically, a non-native time picker issue. Removing ->native(false) from the time picker field also works just fine, which will do fine for now
No description
Jump to solution
22 Replies
tuto1902
tuto1902OP15mo ago
Oh, and I've already ran composer update. My table builder package version is 3.0.53 here's the model
class Medication extends Model
{
use HasFactory;

public $fillable = [
'name',
'times'
];

protected $casts = [
'times' => 'array'
];
}
class Medication extends Model
{
use HasFactory;

public $fillable = [
'name',
'times'
];

protected $casts = [
'times' => 'array'
];
}
and the migration
Schema::create('medications', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->json('times');
$table->timestamps();
});
Schema::create('medications', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->json('times');
$table->timestamps();
});
Anyone around? this is actually holding me back a bit
tuto1902
tuto1902OP15mo ago
oh thank God!
awcodes
awcodes15mo ago
Pretty sure the date / time pickers need a carbon instance to work properly.
tuto1902
tuto1902OP15mo ago
Well, if that's the case, wouldn't it fail to show on a non-simple repeater?
awcodes
awcodes15mo ago
Possibly. I do know that the simple repeater has a different underlying data structure though. So it’s probably just a matter of formatting the data in the right way with formatStateUsing() Or the appropriate hydration method. #awcodes-table-repeater could be a possible solution too if your having troubles with it.
tuto1902
tuto1902OP15mo ago
Ok I'll try out your suggestions and come back with results. Do you mind if I @ you on this thread once I have something to share? If it works you won't hear from me again 😅
awcodes
awcodes15mo ago
Sure. No worries. Feel free. Thank you for asking.
tuto1902
tuto1902OP15mo ago
Ok, I can't do modifyStateUsing on the repeater field because a quick dd on the state reveals that is actually null (image 1)
Forms\Components\Repeater::make('times')
->simple(
Forms\Components\TimePicker::make('time')
->required()
->native(false)
->seconds(false)
->displayFormat('h:i A')
->formatStateUsing(fn ($state) => dd($state))
)
Forms\Components\Repeater::make('times')
->simple(
Forms\Components\TimePicker::make('time')
->required()
->native(false)
->seconds(false)
->displayFormat('h:i A')
->formatStateUsing(fn ($state) => dd($state))
)
Also, using modifyStateUsing on the repeater itself reveals that the structure of the data is a flat array of string values. (image 2)
Forms\Components\Repeater::make('times')
->simple(
Forms\Components\TimePicker::make('time')
->required()
->native(false)
->seconds(false)
->displayFormat('h:i A')
)
->formatStateUsing(fn ($state) => dd($state))
Forms\Components\Repeater::make('times')
->simple(
Forms\Components\TimePicker::make('time')
->required()
->native(false)
->seconds(false)
->displayFormat('h:i A')
)
->formatStateUsing(fn ($state) => dd($state))
I might have to deep dive into the repeater code to figure out where are the values getting lost If that doesn't work I'll move on to the table-repeater. I do feel the documentation is a bit misleading in this case.
No description
No description
tuto1902
tuto1902OP15mo ago
Oh, its a TimePicker issue. Using a TextImput works just fine!
No description
Solution
tuto1902
tuto190215mo ago
More specifically, a non-native time picker issue. Removing ->native(false) from the time picker field also works just fine, which will do fine for now
No description
awcodes
awcodes15mo ago
Was just about to say that. Lol. The js version is expecting a carbon instance. Glad you got it working though.
tuto1902
tuto1902OP15mo ago
right, and it can't figure it out from a JSON column. And I can't really cast the contents of a JSON column contents into a carbon instance
awcodes
awcodes15mo ago
No but you could loop through the array and convert each item as a carbon instance. Probably overkill for the need though.
tuto1902
tuto1902OP15mo ago
Oh, what I can do is move the JSON column to another table and cast that as time
awcodes
awcodes15mo ago
That’s even more unnecessary. Lol. But if it works for you, sure.
tuto1902
tuto1902OP15mo ago
I'll think about it. It's for a simple Youtuve video example so a time picker would do just fine not central to the subject
awcodes
awcodes15mo ago
Could just loop through the state in afterStateHydrated() and set the component->state() as an array of carbon instances.
tuto1902
tuto1902OP15mo ago
Hmm, that's a good idea. I'll try that
awcodes
awcodes15mo ago
What ever works for you.
tuto1902
tuto1902OP15mo ago
Ok marking as closed. Thanks for the help
awcodes
awcodes15mo ago
No worries. Happy coding.
Want results from more Discord servers?
Add your server