Certainly! Allowing users to build custom forms is a common requirement, and it involves creating a flexible, dynamic system. Here’s how you can approach it:
1. Database Design
You’ll need tables to store form structures and user responses. For example:
forms
- id
- name
- created_by
form_fields
- id
- form_id
- label
- field_type (text, textarea, select, radio, checkbox, etc.)
- options (nullable; JSON for select, radio, checkbox choices)
- required (boolean)
form_responses
- id
- form_id
- submitted_by
- submitted_at
form_response_values
- id
- form_response_id
- form_field_id
- value (string)
2. UI to Build the Form
Use a front-end framework (like Vue or React) or regular Blade templates with some JS to allow users to:
- Add/remove fields
- Choose field type
- Specify label and options
Save the structure to the database.
3. Display Form for Input
When someone needs to fill out the form:
- Load the form fields from the database.
- Loop through them to generate the inputs dynamically.
Example in Blade:
@foreach ($form->fields as $field)
<label>{{ $field->label }}</label>
@if ($field->field_type == 'text')
<input type="text" name="fields[{{ $field->id }}]" />
@elseif ($field->field_type == 'select')
<select name="fields[{{ $field->id }}]">
@foreach (json_decode($field->options) as $option)
<option value="{{ $option }}">{{ $option }}</option>
@endforeach
</select>
@endif
@endforeach
4. Store Form Responses
In your controller:
public function store(Request $request, Form $form)
{
$response = $form->responses()->create([
'submitted_by' => auth()->id(),
'submitted_at' => now(),
]);
foreach ($request->input('fields') as $field_id => $value) {
$response->values()->create([
'form_field_id' => $field_id,
'value' => is_array($value) ? json_encode($value) : $value,
]);
}
// Redirect or return response
}
5. Generate Reports
Query form_responses and form_response_values, then transform the data for reporting.
Example:
$results = FormResponse::where('form_id', $formId)
->with('values.field')
->get();
You can then aggregate or export this data as needed.
Summary
- Store forms and fields as dynamic records
- Build a front-end to let users design forms
- Render forms dynamically for input
- Store responses tied to the form/fields
- Query responses for reporting
This approach is highly flexible and is how most "form builder" tools work. Consider using packages like Laravel-form-builder or Nova Flexible Content as a starting point if you need more.
Let me know if you need concrete examples for any part!