Form: Adding user-defined pivot data to belongsToMany relationship

I have a form like this:
public static function form(Form $form): Form
{
return $form
->schema([
CheckboxList::make('allowedTargets')
->disabledOn('edit')
->relationship(name: 'allowedTargets', titleAttribute: 'name')
->pivotData([
TextInput::make('allowed_file_extensions')
->label('Allowed File Extensions')
->required(),
])
]);
}
public static function form(Form $form): Form
{
return $form
->schema([
CheckboxList::make('allowedTargets')
->disabledOn('edit')
->relationship(name: 'allowedTargets', titleAttribute: 'name')
->pivotData([
TextInput::make('allowed_file_extensions')
->label('Allowed File Extensions')
->required(),
])
]);
}
allowedTargets is a many-to-many-relationship of my Model. Each target has a pivot column allowed_file_extensions which accepts an array of file extensions as strings. Using a TextInput inside the pivotData()-method is not doing anything. But it is also not documented and was just an example what I already tried. I want the user be able to add allowed_file_extensions as pivot data on the create page of my Model. So he is able to create the parent model directly with the relationship and the specific pivot data. Is that possible? I also tried it using a RelationManager but it seems like RelationManagers don't show up on the resource create page.
1 Reply
Nuekrato
NuekratoOP7mo ago
For now I would like to go with a RelationManager on the Edit page so I can edit the pivot data later. But I have problems using a repeater:
[2024-05-15 10:18:48] production.ERROR: foreach() argument must be of type array|object, string given {"userId":"9bb5ab66-79fe-4a37-a059-07bc95238656","exception":"[object] (ErrorException(code: 0): foreach() argument must be of type array|object, string given at /var/www/html/vendor/filament/forms/src/Components/Repeater.php:755)
[stacktrace]
#0 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(255): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError()
#1 /var/www/html/vendor/filament/forms/src/Components/Repeater.php(755): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->Illuminate\\Foundation\\Bootstrap\\{closure}()
[2024-05-15 10:18:48] production.ERROR: foreach() argument must be of type array|object, string given {"userId":"9bb5ab66-79fe-4a37-a059-07bc95238656","exception":"[object] (ErrorException(code: 0): foreach() argument must be of type array|object, string given at /var/www/html/vendor/filament/forms/src/Components/Repeater.php:755)
[stacktrace]
#0 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(255): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError()
#1 /var/www/html/vendor/filament/forms/src/Components/Repeater.php(755): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->Illuminate\\Foundation\\Bootstrap\\{closure}()
This is the form in my AllowedTargetsRelationManager:
public function form(Form $form): Form
{
return $form
->schema([
Repeater::make('allowed_file_extensions')
->simple(
TextInput::make('extension')
->regex('/\s/u')
->required(),
)
]);
}
public function form(Form $form): Form
{
return $form
->schema([
Repeater::make('allowed_file_extensions')
->simple(
TextInput::make('extension')
->regex('/\s/u')
->required(),
)
]);
}
These are typical data in the database of the allowed_file_extensions column: ["json"]`` default: []` Anyone has an idea? I also have a pivot model that casts the JSON column to an array:
protected $casts = [
'allowed_file_extensions' => 'array',
];
protected $casts = [
'allowed_file_extensions' => 'array',
];
Want results from more Discord servers?
Add your server