How to Use the Same Form for Add and Edit in Laravel

If you have to develop create and update forms in Laravel, you can use a single blade file for both create and update instead of creating two different blade files. We can use the same form for Add and Edit. In this case, you maintain only one blade file without adding much complexity to the form.

We will develop a demo application to show this. We will show a list of product categories and we will add a new category and update an existing category. We will use the same form both add category and update category.

Use same form for Add and Edit in Laravel Use same form for create and update in Laravel Same form for Add and Edit in Laravel

Step 1 - Create a Laravel project

Let us create a project in Laravel 10. Make sure you have Composer and PHP installed in your system. We will use the below command to create the project named lara_form.

This will create a folder named "lara_form" under the folder where you run the above command.

Step 2 - MySQL Database and Migration

Update .env file for database details. I am using MySQL database named 'lara_form', so I updated the file as below:

We will create a custom table called 'categories' to store product category name and description. Let us create a migration for this table. Go to your project folder and run the below command to create migration from VS code terminal.

Above command will create the migration file name <yyyy_mm_dd_xxxxxx>_create_categories_table.php under database/migrations folder and a Model named "Category" under app/Models folder. Here yyyy_mm_dd_xxxxxx is the datetime when you created the migration file. Open the migration file for categories table and update the up() method as below:

Migration scripts for categories table

So, we are creating a new table called 'categories' with the column names as 'name' and 'descr'

Let us now run the Laravel migration to create the new table as well as the default tables for Laravel. Run below command from the project folder to create the tables:

After running the migration, below tables will be created.

laravel migration file

Update Model

Category model is already created, let us update it to add $fillable columns. In our case, these are 'name' and 'descr' columns.

Step 3 - Create a Resource Controller

We will create a resource controller named "CategoryController". The resource controller will create the methods we need for our demo application. index(), create(), store(), edit() and update() methods will be available in our controller.

Let us run below command from the terminal to create the resource controller:

Create a Resource Route

Let us add a resource route in web.php, we will give the name as category.

The following routes will be available for us:

laravel add and edit form routes

We will show a list of all product categories in one view. Within this view, we will have an Edit button for each category. Also, in the same view will have an Add button to add a new category. The category listing screen is given below:

laravel add and edit form, index view

Let us write the code for index(), create(), store(), edit() and update() methods in the controller.

index() method will select all rows from the categories table and call the view 'index' to show list of all categories.

create() method will be used to add category and it will call the view 'add_category'. Add category form will have only two input fields - category name and description.

store() method will validate the form data and insert a row in the categories table.

edit() method will be used to edit category name and descr, it will call the same view 'add_category' for the edit form.

update() method will update category name and description.

So, we will have two views - index and add_category. The add_category view will be used for both add category and edit category.

Methods in the controller are given below:

In the store() method, if a category is added successfully, the user is redirected to the index page with a success message. But in case of error, an error message is displayed and the user stays in the same page. This is true for update() method as well.

Step 4 - Laravel blade views for add and update forms

We will be using an index view and another view for both add and update category. Below are the views along with the layout:



Add and Edit View

Since we will be using the same view for Add and Edit, we need to add some code in the Add form so that we can use it as an update form as well.

Heading in the form

We need to use if...else here to check if the view is called for edit or add. For edit mode we will have category ID value set, we will use the below code to give a heading for the form.

This means, if the category id is available, then use the form heading "Update Product Category", else use "Add Product Category". In the same way, we will use the appropriate routes for the form action and the input value attribute in the form. See below:

Form Action

Form Input Value

Below is the complete code for add_category view:


Below is the stylesheet:


Step 5 - Test the application

From the project root, start the php development server:

From the browser run localhost:8000/category. You will see a list of categories displayed, you will not have the data in the table initially, so you will see "No Data found". Click on the Add Category button and add a new category. You will see the new category added to the list. Now update the same and check if everything is working fine.

download source code for Laravel add and edit form Download Source code from github.

 Laravel add and edit formWatch YouTube Video