Insert Form Data Into Database Using Laravel | Save Form Submit

Learn how to Insert Form Data Into Database Using Laravel in this article.

We will create one form in this tutorial and we will save this form data into MySQL database.

A submit button will be used to insert or submit form data into database using laravel.

We will use MySQL database. Here you can read on installing MySQL database with laravel and sequel pro.

Last view of tutorial

Below video shows what we will develop today.

Step 1. Create devices table

First of all create a new laravel project using below command.

  • It will create a new project named allrecordform.

Now create a new table using the following command

  • After this command, you will find one migration file in database->migrations directory.

This file should contain the below source code

Inside Schema::create() method, I have added below two lines

  • These two lines will create two columns in table named “name” and “description.

Now run migrate command as per the below

  • After this, a new table named “devices” will be created in your database.

Step 2. Defining routes

You should have a file called web.php in routes directory.

Write down the below source code in this file.

  • I have defined three routes in this file. When you fire http://127.0.0.1:8000/devices in browser, system will trigger first route. It will call index() method in DevicesController.php file. (we will create this file later)
  • Similarly, other two routes will work according to their definition. (create() method and storeDevice() method for second and third route respectively)

Step 3. Creating a Model class

To use eloquent ORM while working with database, we need to have one model class for each table.

Here we have created a table named devices. A name of model class should be the singular form of tables’s name.

So our model’s name should be “Device.”

Run the below command to make Device model.

  • With the help of above command, System will generate new file Device.php in app directory. Source code for this model class is as the below

Step 4. Generation of Controller Class

In route file (web.php) we have defined some routes which were referring to the DevicesController.php file

So let us create this controller file. Fire the below command

  • Now check your app->Http->Controllers directory. There should be a file  DevicesController.php file here.

Add the below code structure in DevicesController.php file

This file contains three methods which will decide the flow of our laravel project.

First of all, read the below code

  • index() method will first get all the data from “devices” table.
  • Compiler will store all these data into the variable called $devices.
  • Then it will return one view file (index.blade.php). A file is located in the resources->views->devices directory. (By default, we have only resources->views directory. Inside views, we will create devices folder later)
  • There is a second parameter in the return statement. ( compact’devices’ ). These will pass the variable $devices in view file called index.blade.php that we will create in next step.

Now attend the following code

  • This method is very simple. It is just returning one view file called create.blade.php which we will also create in next step.

Now see the last method.

  • First of all, compiler will create a new variable $device with help of model class “Device.”
  • Compiler will run this method when the user clicks the “Submit” button on the form. Here, it will also get two parameters (name and description.) These two will contain the values entered by the user.
  • Then it will fetch the value of the parameter “name” and will store it in $device->name
  • $device->description will have the value of description parameter.
  • Finally, A line $device->save() will insert the form data into the devices table.
  • At last it will return a redirect call to /devices route.

Step 5. View files

Now final task is to create blade view files.

First of all, create a new folder named “devices” inside resources->views directory.

Inside this devices folder, make a new php file named index.blade.php

Code snippet for index.blade.php is as the below

  • Above code contains three @foreach loops.
  • This file will show all the rows of the devices table using first @foreach loop.
  • Second loop will print only names of the devices and the last one will print desription of the device.

Now make a new file named create.blade.php in resources->views->devices directory.

Source for this create.blade.php is as the below

  • This view file is responsible for form creation. When user visits http://127.0.0.1:8000/devices/create url, compiler will execute above source code.
  • User will show one form which have two input fields : name and description.

See the below line

  • A method is post and action is /devicesaction. Means that when the user clicks the submit button, compiler will execute /devicesaction route in web.php file (see step 2.)
  • From here compiler will execute storeDevice() method from DevicesController.php file. (see step 4.)

After this much of coding, if you run your project, you should get the output as shown in youtube video at the starting of this article.

Download Code for insert form data into database using laravel

https://github.com/1hardikparsania/Insert-Form-Data-Into-Database-Using-Laravel-Save-Form-Submit

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *