Insert Data into a Database with Laravel 5

Laravel is a free, open-source, PHP web framework. It follows the MVC (Model-View-Controller) architectural pattern. Laravel’s command line interface (CLI), named Artisan, provides functionality that aids in building Laravel-based applications. The latest stable version of Laravel is 5.4. This release has many new features, including Laravel Dusk, Laravel Mix, Blade Components and Slots, Markdown Emails, Automatic Facades, Route Improvements, Higher Order Messaging for Collections, and many others.

Database Configuration

Laravel database configuration is done using the .env file in your project folder. The configuration can be defined as follows in the relevant fields.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=yourDatabaseName
DB_USERNAME=yourDatabaseUsername
DB_PASSWORD=yourDatabasePassword

Files required to insert data into the database

Three files would be required to insert files into the database, namely; controller file, model file and view file. In this example I have presented how to add some Employee details into a table called ‘Employee’ which has the following structure.

Screen Shot 2017-05-12 at 12.39.05 PM

The controller file (employeeController.php) can be created using the following command on the terminal. This will be created in the App –> Http –> Controllers folder.

php artisan make:controller employeeController

The model file (addEmployee_model.php) can be created as follows. This will be created in the App folder.

php artisan make:model addEmployee_model

Apart from these two, a view file will be needed which should be in the Resources –> Views folder. This can be created by right clicking on the views folder and selecting  New–> File. Name the view file as addEmployee.blade.php.

View code

<form class="form-horizontal " method="POST" action="/add">
<div class="form-group">
<div class="col-md-1"></div>
<label class="col-md-2 col-sm-2 control-label">Employee ID*</label>
<div class="col-md-6 col-sm-10">
            <input type="text" class="form-control" name="E_id"></div>
</div>
<div class="form-group">
<div class="col-md-1"></div>
<label class="col-sm-2 control-label">Full Name*</label>
<div class="col-md-6 col-sm-10">
            <input type="text" class="form-control" name="Name"></div>
</div>
<div class="form-group">
<div class="col-md-1"></div>
<label class="col-sm-2 control-label">Email</label>
<div class="col-md-6 col-sm-10">
            <input type="email" class="form-control" name="Email"></div>
</div>
<div class="form-group">
<div class="col-md-1"></div>
<label class="col-sm-2 control-label">Job Role</label>
<div class="col-md-6 col-sm-10">
            <input type="text" class="form-control" name="Job_Role"></div>
</div>
<div class="form-group">
<div class="col-md-1"></div>
<label class="col-sm-2 control-label">Address</label>
<div class="col-md-6 col-sm-10">
            <input type="text" class="form-control" name="Address"></div>
</div>
<div class="form-group">
<div class="col-md-1"></div>
<label class="col-sm-2 control-label">NIC No.</label>
<div class="col-md-6 col-sm-10">
            <input type="text" class="form-control" name="NIC"></div>
</div>
<div class="form-group">
<div class="col-md-1"></div>
<label class="col-sm-2 control-label">Contact No.*</label>
<div class="col-md-6 col-sm-10">
            <input type="text" class="form-control" name="Contact"></div>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}"/>

    <button style="float: left" type="reset" class="btn btn-default">Clear Form</button>
    <button style="float: right" type="submit" class="btn btn-primary">Submit</button>
</form>

The code above represents a simple form which takes 3 input values (E_id, Name and Email). The action of the form is defined as “/add” with the POST method. The route for the action should be included in the file, routes –> web.php, which will be as follows.

Route::post('add', 'employeeController@add');

Once the submit button of the form is clicked, the employeeController class will be called along with the method “add”. The controller code for the particular action is as follows.

Controller code

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Requests;
use App\addEmployee_model; //Including the model in the controller file

class employeeController extends Controller
{

public function addEmployee(){
return view('addEmployee');
}

public function add(Request $request){

$e_id = $request->get('E_id');
$name = $request->get('Name');
$email = $request->get('Email');

$addEm = new addEmployee_model();
$addEm -> E_id = $e_id;
$addEm -> Name = $name;
$addEm -> Email = $email;

$addEm-> save();

return redirect()->action('employeeController@addEmployee');

}
}

The namespace refers to the location of the particular file. This is included in every file in Laravel. The “add” function corresponds to the “adding an employee” functionality.

$addEm is an instance of the model we use. $addEm -> save() would save the data to the database.

Next we would need the model to map these data correctly to the table fields. The model code is as follows.

namespace App;

use Illuminate\Database\Eloquent\Model;

class addEmployee_model extends Model
{
    //To disable default timestamps by laravel since there are no such field in the table
   public $timestamps = false;

   //Define the table
   protected $table = "Employee";

   //Define primary key
    protected $primaryKey = "id";

    protected $fillable = ['E_id', 'Name', 'Email'];
}

The code to insert a data into a database in Laravel 5 is complete.

Please comment any queries you have and I’d be happy to provide an answer at my earliest.

Thank You!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: