Foreach error with repeater field

Hi, i have strange behaviour with a repeater field (never encountered before) i have a simple repeater
Forms\Components\Repeater::make('links')
->label('Links')
->maxItems(1)
->addActionLabel('Add button')
->schema([
Forms\Components\TextInput::make('url')
->required()->url(),
Forms\Components\TextInput::make('text')
->required(),
]),
Forms\Components\Repeater::make('links')
->label('Links')
->maxItems(1)
->addActionLabel('Add button')
->schema([
Forms\Components\TextInput::make('url')
->required()->url(),
Forms\Components\TextInput::make('text')
->required(),
]),
when i create a new item, it works when i try to edit an exhisting item, populated with a seeder i have the error 'foreach() argument must be of type array|object, string given' the field is a longtext field, with cast as array (but if i change cast to json is the same) in the database, it saved like this (new from form)
[{"url":"https:\/\/www.google.com","text":"test"}]
[{"url":"https:\/\/www.google.com","text":"test"}]
while the seeder save like this

"[{\"url\":\"https:\\\/\\\/www.google.com\",\"text\":\"test\"}]"

"[{\"url\":\"https:\\\/\\\/www.google.com\",\"text\":\"test\"}]"
Solution:
I would use an array in the seeder... you have the cast setup in your model?
Jump to solution
3 Replies
Soundmit
Soundmit2mo ago
the seeder

\App\Models\MessageTemplate::create([
'key' => 'quoteRejected',
'type' => 'internal',
'name' => 'Quote rejected',
'subject' => 'Oh no! Hai rifiutato il preventivo!',
'preheader' => 'Ti aspettiamo in laboratorio per il ritiro del tuo strumento',
'content' => '<p>Gentile {!! $customerName !!},</p><p>hai rifiutato il preventivo.</p><p>Ti preghiamo di venire a ritirare <b>{!! $itemType !!} {!! $item !!}</b> entro 15 giorni dal ricevimento di questa email. Siamo a disposizione per qualsiasi chiarimento</p>',
'links' => '[{"url":"https:\/\/www.google.com","text":"test"}]'
]);

\App\Models\MessageTemplate::create([
'key' => 'quoteRejected',
'type' => 'internal',
'name' => 'Quote rejected',
'subject' => 'Oh no! Hai rifiutato il preventivo!',
'preheader' => 'Ti aspettiamo in laboratorio per il ritiro del tuo strumento',
'content' => '<p>Gentile {!! $customerName !!},</p><p>hai rifiutato il preventivo.</p><p>Ti preghiamo di venire a ritirare <b>{!! $itemType !!} {!! $item !!}</b> entro 15 giorni dal ricevimento di questa email. Siamo a disposizione per qualsiasi chiarimento</p>',
'links' => '[{"url":"https:\/\/www.google.com","text":"test"}]'
]);
Solution
Tally
Tally2mo ago
I would use an array in the seeder... you have the cast setup in your model?
Soundmit
Soundmit2mo ago
yes, now i have changed the seeder like this

\App\Models\MessageTemplate::create([
'key' => 'sendQuote',
'type' => 'system',
'name' => 'Send quote',
'subject' => 'Il tuo preventivo è pronto, per favore conferma!',
'preheader' => 'Accetta o rifiuta il tuo preventivo',
'content' => '<p>Gentile {!! $customerName !!},</p><p>siamo lieti di informarti che il preventivo per <b>{!! $itemType !!} {!! $item !!}</b> è pronto.</p><p>Clicca il link presente in questa email per accedere al tuo profilo ed accettarlo. Siamo a disposizione per qualsiasi chiarimento o per discutere il preventivo con te in modo più approfondito</p>',
'links' => [
[
'url' => 'https://www.google.com',
'text' => 'test'
]
]
]);

\App\Models\MessageTemplate::create([
'key' => 'sendQuote',
'type' => 'system',
'name' => 'Send quote',
'subject' => 'Il tuo preventivo è pronto, per favore conferma!',
'preheader' => 'Accetta o rifiuta il tuo preventivo',
'content' => '<p>Gentile {!! $customerName !!},</p><p>siamo lieti di informarti che il preventivo per <b>{!! $itemType !!} {!! $item !!}</b> è pronto.</p><p>Clicca il link presente in questa email per accedere al tuo profilo ed accettarlo. Siamo a disposizione per qualsiasi chiarimento o per discutere il preventivo con te in modo più approfondito</p>',
'links' => [
[
'url' => 'https://www.google.com',
'text' => 'test'
]
]
]);
and it works