Creating multiple records with a single laravel filament form
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Creating Multiple Records with a Single Filament Form: A Deep Dive
As developers building complex interfaces with frameworks like Laravel and Filament, we often encounter scenarios where a single form submission needs to result in multiple related database entries. Your situation—using RichEditor fields and needing to generate separate records for each piece of content—is a very common requirement when dealing with rich text or structured data input.
The challenge you are facing lies in efficiently mapping the nested data from your Filament form structure into multiple Eloquent creations within the save process. While diving into custom handlers like handleRecordCreation is possible, there are often more elegant and maintainable ways to handle this relationship, which align better with Laravel's principles of separation of concerns.
This post will explore why your current approach works but also present a cleaner, more scalable solution using robust Eloquent patterns.
The Challenge: Mapping Rich Text to Database Records
You have a form that captures rich text content (e.g., 'steps' and 'goal'). You want these two large text blocks to be stored as separate, normalized records linked back to the main parent record (like Quiz). Your attempt to loop through the data in your createRecord class is fundamentally correct, but ensuring it returns the expected result and handles potential model constraints efficiently is key.
The core issue often isn't how you loop, but where and how you execute those database calls within the Filament lifecycle.
The Recommended Approach: Leveraging Eloquent Mass Assignment and Relationships
Instead of forcing complex logic into a single createRecord handler, a more robust approach involves leveraging Laravel’s built-in capabilities for bulk operations or ensuring your model structure supports these relationships cleanly. Since you are dealing with creating dependent records, we need to ensure the creation is atomic and related correctly.
If your goal is to create new models (let's assume they belong to a FieldRecord model) based on the input data, you should focus on making the relationship explicit rather than relying solely on manual iteration inside the resource class.
Implementing Efficient Record Creation
When performing bulk creation based on form inputs, ensure your data preparation is clean. If you are creating records that belong to the current parent record being saved (e.g., a Quiz), you should leverage Eloquent’s relationship methods directly rather than manually constructing arrays for mass assignment if possible.
Here is how you can refine the logic within your save mechanism, ensuring you correctly handle the creation of related entities:
// Example within your Resource's createRecord method or custom Action
protected function handleRecordCreation(array $data): FeedbackReport
{
$quiz = static::getModel(); // Assuming 'static::getModel()' returns the parent model (e.g., Quiz)
foreach ($data as $field_name => $value) {
// Only process fields that require a separate record, ignoring IDs like quiz_id if they are handled elsewhere
if ($field_name !== 'quiz_id') {
// Create the related record directly using the parent model instance.
$record = $quiz->fields()->create([ // Assuming you have a relationship defined: hasMany(FieldRecord::class)
'field_name' => $field_name,
'value' => $value,
]);
// Log or manage creation results if necessary
}
}
// Return the standard success object expected by Filament
return new FeedbackReport($quiz);
}
Key Takeaways from this approach:
- Relationship First: Define a clear relationship between your main model (e.g.,
Quiz) and the records you want to create (e.g.,FieldRecord). This is core to good Laravel design, as demonstrated by patterns frequently seen in projects built on frameworks like those promoted by laravelcompany.com. - Eager Loading/Creation: Instead of relying solely on static calls (
static::getModel()::create($record)), use the model instance you are currently saving to establish context (e.g.,$quiz->fields()->create(...)). This ensures data integrity and proper foreign key assignment.
Conclusion: Structure Over Manual Looping
While manually looping through form data is technically achievable, it often obscures the relationship between your models. For complex data structures like rich text fields, the most maintainable solution involves establishing a clear Eloquent relationship and then using that relationship to perform bulk or nested creations. This keeps your code aligned with the object-oriented principles of Laravel, making future maintenance significantly easier than relying on procedural loops within form handlers. Focus on defining what is being saved (the relationship) rather than just how it is being saved (the loop).