1. Create Migration for the Form Data
First, let's create a migration for the data you will store in the database. We will create a table for employee records that include fields like empname
, phone
, and department
.
Run the following command to create a migration:
This will create a new migration file in database/migrations/
. Open that file and define the table schema:
Migration File (database/migrations/xxxx_xx_xx_create_employee_records_table.php
):
2. Run Migration
Once the migration is ready, run the following command to create the employee_records
table in your database:
3. Create Model for EmployeeRecord
Now, let's create a model that corresponds to the employee_records
table.
Run the following command:
This will create a model file in app/Models/EmployeeRecord.php
. Open it and ensure it looks like this:
Model File (app/Models/EmployeeRecord.php
):
This model will allow you to interact with the employee_records
table.
4. Controller to Handle the Form
Now, let's create a controller that will handle the form display and form submission.
Run the following command to create the controller:
Then, define the index
method to show the form and the saveRecord
method to process the form data.
Controller File (app/Http/Controllers/FormController.php
):
Explanation of saveRecord
method:
-
Validation: The form data is validated using the
validate
method. This ensures that theempname
,phone
, anddepartment
fields are required, and their lengths are restricted. -
Saving Data: After validation, each employee record is saved into the
employee_records
table. The loop goes through each employee and inserts the corresponding data. -
Redirect: After saving the data, it redirects the user back with a success message using
session()->flash
.
5. Update Web Routes
In your routes/web.php ,
Define the routes to display and handle the form submission:
Web Routes (routes/web.php
):
This way, the form will show when you visit /form
and the form will be submitted to the same route via a POST
request.
6. Create the Blade View for the Form
Now, let's create the form view where users can input the employee records.
Blade File (resources/views/form.blade.php
):
Explanation of Blade View:
-
Success Message: If there's a success message in the session (from
session()->flash()
), it will be displayed in a green alert box. -
Dynamic Rows: The
#add-row
button lets users add multiple employee rows. Each row contains fields for the employee's name, phone number, and department. -
Remove Row: The
remove-row
button allows users to remove a row if they no longer want to include that employee.
7. Final Step: Run the Application
Now that everything is set up, run your Laravel app:
Visit http://localhost:8000/form
to see your form in action. Add multiple employee records, submit the form, and verify that the records are saved to the database.
Your Laravel Form is Ready!
You have successfully:
-
Created a form to input employee records.
-
Validated the data before saving it.
-
Saved the data to the database.
-
Displayed a success message after form submission.