The MVC framework has made it easy to integrate database. It currently supports only 4 major databases which are MySQL, Postgres, SQLite, and SQL Server.You can fire a query to any of these databases by the use of raw SQL, Eloquent ORM, and the fluent query builder. Here, we will be focusing mainly on the 4 operations that can be performed on the databases which include Create, Read, Update, and Delete, in short CRUD.

For your better understanding, below is an example.
Here, we are creating a basic student management database. You can find this database.php file in the config directory. To start with this database, first, create a college database with the help of MySQL. Create the table as below provided structure.

Name of Database: collegeName of Table: student

Column Name

Column Datatype

Remarks

ID

int(11)

Primary Key (PK) / Auto Increment

Name

varchar(25)

Now, take a brief look at how to perform the 4 primary operations (create, read, update, and delete) on the student table.

[post_middile_section_ad]

How to Insert Records?
With the help of this operation, you can insert a record in a pre-existing database. Follow the given syntax strictly when using an insert() method. Below are the syntax and a few important information to use insert method properly.

Syntax

bool insert(string $query, array $bindings = array())

Parameters

* $query(string) – to fire a query in a database
* $bindings(array) – to bind values with queries

Returns

bool

Description

You can insert a record in a database

Follow the example to get a clear picture of how to use insert method.Step 1: Create a plain controller by the name StudInsertController.laravel> php artisan make:controller StudInsertController –plain

Step 2: After the successful execution of the command, include the code provided below. It should be inserted in the StudInsertController.php file. To locate this file, follow the path: app/Http/Controllers/.<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;use DB;use App\Http\Requests;use App\Http\Controllers\Controller;

Step 3: Now, you need to create a view file called stud_create.php. Save this file in the path: resources/views/. In this file, copy the below provided piece of code.<html><head><title> Student Management | Add </title></head>

Step 4: After this, you are required to open routes.php file. Here, add the following content. You can find this file in the path: app/Http/.Route::get(‘insert’,’StudInsertController@insertform’);Route::post(‘create’,’StudInsertController@insert’);

Step 5: Now, to check if it is working properly or not. Copy the URL: http://localhost:8000/insert to the web server. And you will get an output like the following.

How To Retrieve Records?
This operation will work only when you have successfully configured the database. Well, to retrieve records from a configured database, you can use select() method. Below is the syntax for select() method with a few relevant information.

To understand select() method, take a look at the following example.Step 1: Create a controller named as StudViewController. Note that, it is a plain constructor.laravel> php artisan make:controller StudViewController –plain

Step 2: After the successful creation of the controller, Include the given lines of code in StudViewController.php file. You can locate this file at app/Http/Controllers/.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

Step 4: Now, copy the following line of code in the routes.php file. You can find this file in app/Http/ path.Route::get(‘view-records’,’StudViewController@index’);

Step 5: To see the final output, copy the given URL: http://localhost:8000/view-records to the PHP’s web server. This query will let you see the stored records from the database.

How To Update Records?
You can easily update pre-existing records in a database with the help of update() method. You can take a look at the table given below. It contains the syntax and other useful information related to update() method.

Here is an example to learn about update() method more clearly.Step 1: Execute the given command to create a controller with the name StudUpdateController.laravel> php artisan make:controller StudUpdateController –plain

Step 2: After the successful execution of the command, copy the given code to the StudUpdateController.php file. You can find this file on the path: app/Http/Controllers/.<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;use DB;use App\Http\Requests;use App\Http\Controllers\Controller;

Step 5: The last piece of code is to be added to routes.php. You can find this file at app/Http/.Route::get(‘edit-records’,’StudUpdateController@index’);Route::get(‘edit/{id}’,’StudUpdateController@show’);Route::post(‘edit/{id}’,’StudUpdateController@edit’);

Step 6: Now, to update the records in a database, copy the URL: http://localhost:8000/edit-records to the web server and you will be displayed with the following output.

Step 7: Click the edit link to whichever record you want to edit. After which, you will be redirected to the page where you can edit the record.

After the successful operation on the record, you will get the following output.

How To Delete Records?
The delete() method helps you to delete records from an already configured database. You can take a look at the syntax of the delete() method before we proceed further.

Here is an example for you to get a clearer picture of delete() method and its use.Step 1: Start with the creation of StudDeleteController.laravel> php artisan make:controller StudDeleteController –plain

Step 2: After the successful creation of the controller, copy the following code to the StudDeleteController.php. This file is stored in app/Http/Controllers/.<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;use DB;use App\Http\Requests;use App\Http\Controllers\Controller;

Step 4: Now, to finish the whole process, include the following code the routes.php file. You will get this file in app/Http/.Route::get(‘delete-records’,’StudDeleteController@index’);Route::get(‘delete/{id}’,’StudDeleteController@destroy’);