Insert data in MySQL table using Laravel

  • Post author:
  • Post category:Laravel
  • Post comments:0 Comments
  • Post last modified:October 5, 2020
  • Reading time:4 min(s) read
In this blog, you’ll get to find out how to insert data in MySQL table using Laravel. If you’re a beginner I’ll suggest you check all the Laravel related blogs and our Youtube tutorial videos.

Now, Let’s come to the purpose of a way to data insertion using Laravel. within the previous blog and tutorial videos, you’ve got learned a way to create models and controllers for your project and therefore the database connection also. Please find the video tutorial given below for an improved understatement.

Step-1: Create a blade (example.blade.php) code is given below:

@extends('master')

@section('content')
<br>
<div class="container">
<div class="card">
<div class="card-body">
@if(session()->get('success'))
<div class="alert alert-success alert-dismissible fade show" role="alert">

{{ session()->get('success') }}

<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>

@endif


<form action="add_data" method="post">
<!-- you have to use csrf token here -->
@csrf
<div class="row">
<div class="col-md-6">
<label>Name</label>
<input type="text" name="uname" class="form-control" required>
</div>
<div class="col-md-6">
<label>Email</label>
<input type="text" name="uemail" class="form-control" required>
</div>
<div class="col-md-12">
<label>Address</label>
<input type="text" name="uadd" class="form-control" required>
</div>
</div>
<br>
<button type="submit" class="btn btn-primary">Save</button>
</form>
</div>
</div>
</div>

@stop

Whenever you are creating a form don’t forget to add @csrf(cross-site request forgery token) within the form tag otherwise you will getting error code 419 while you submit your form. If you are not using the master blade then, please ignore @extends(‘master’),@section(‘content’), and @stop tags.

Step-2: To load the view blade create a function to your respective controller as shown in the below code:

public function registration()
    {
      return view('registration');
    }
I will add the source code of the route file below.

Step-3: Create a table in your respective database. While you are creating a table don’t forget to add two additional columns/attributes and their name will be “created_at” and “updated_at”. One more thing the primary key or so-called auto-increment column name has to be “id” and it can not be anything else.

After creating the table go to your respective model and then set you table name in a protected variable as shown in the below code:

class Emp_model extends Model
{
   //set your table name here

	protected $table="users"; //users is my table table name
}
insert data in MySQL table using Laravel

Step-4: Now create a function to your controller where you will be submitting your form and insert the respective data of your form into the table as shown in the below code:

 public function data_insert(Request $r) //Request is a method and $r is it's object
    {
      $name=$r->uname;
      $email=$r->uemail;
      $address=$r->uadd;


      $users= new App\Emp_model;

      $users->name=$name;
      $users->email=$email;
      $users->address=$address;


      $insert=$users->save();

      if($insert){

        return redirect('/reg')->with('success','Data added successfully!');
    }

In the above-mentioned code, the highlighted line is to create an object of our model.but, before you need to add the below line to your controller before declaring the class.

use App;

route file [web.php] code is given below.

Route::get('/','[email protected]');

Route::get('/about','[email protected]');

Route::get('/reg','[email protected]');

Route::post('/add_data','[email protected]_insert');

Here is the complete code of controller and model file.

Controller(Employee.php):

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App;
class Employee extends Controller
{
public function home($value='')
{
return view('a');
}
public function xx()
{
return view('about');
}

//first create a view form.

public function registration($value='')
{
return view('registration');
}

//Now create a function to insert data into database

public function data_insert(Request $r) //Request is a method and $r is it's object
{
$name=$r->uname;
$email=$r->uemail;
$address=$r->uadd;

$users= new App\Emp_model;
$users->name=$name;
$users->email=$email;
$users->address=$address;
$insert=$users->save();
if($insert){
return redirect('/reg')->with('success','Data added successfully!');
}
}
}

Model (Emp_model.php):

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Emp_model extends Model
{
   //set your table name here
  protected $table="users";
}

Leave a Reply