
- Laravel Tutorial
- Laravel - Home
- Laravel - Overview
- Laravel - Installation
- Laravel - Application Structure
- Laravel - Configuration
- Laravel - Routing
- Laravel - Middleware
- Laravel - Namespaces
- Laravel - Controllers
- Laravel - Request
- Laravel - Cookie
- Laravel - Response
- Laravel - Views
- Laravel - Blade Templates
- Laravel - Redirections
- Laravel - Working With Database
- Laravel - Errors & Logging
- Laravel - Forms
- Laravel - Localization
- Laravel - Session
- Laravel - Validation
- Laravel - File Uploading
- Laravel - Sending Email
- Laravel - Ajax
- Laravel - Error Handling
- Laravel - Event Handling
- Laravel - Facades
- Laravel - Contracts
- Laravel - CSRF Protection
- Laravel - Authentication
- Laravel - Authorization
- Laravel - Artisan Console
- Laravel - Encryption
- Laravel - Hashing
- Understanding Release Process
- Laravel - Guest User Gates
- Laravel - Artisan Commands
- Laravel - Pagination Customizations
- Laravel - Dump Server
- Laravel - Action URL
- Laravel Useful Resources
- Laravel - Quick Guide
- Laravel - Useful Resources
- Laravel - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Laravel - Insert Records
We can insert the record using the DB facade with insert method. The syntax of insert method is as shown in the following table.
Syntax | bool insert(string $query, array $bindings = array()) |
Parameters |
|
Returns | bool |
Description | Run an insert statement against the database. |
Example
Step 1 − Execute the below command to create a controller called StudInsertController
php artisan make:controller StudInsertController --plain
Step 2 − After the successful execution of step 1, you will receive the following output −

Step 3 − Copy the following code to file
app/Http/Controllers/StudInsertController.php
app/Http/Controllers/StudInsertController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use DB; use App\Http\Requests; use App\Http\Controllers\Controller; class StudInsertController extends Controller { public function insertform() { return view('stud_create'); } public function insert(Request $request) { $name = $request->input('stud_name'); DB::insert('insert into student (name) values(?)',[$name]); echo "Record inserted successfully.<br/>"; echo '<a href = "/insert">Click Here</a> to go back.'; } }
Step 4 − Create a view file called resources/views/stud_create.php and copy the following code in that file.
resources/views/stud_create.php
<html> <head> <title>Student Management | Add</title> </head> <body> <form action = "/create" method = "post"> <input type = "hidden" name = "_token" value = "<?php echo csrf_token(); ?>"> <table> <tr> <td>Name</td> <td><input type='text' name='stud_name' /></td> </tr> <tr> <td colspan = '2'> <input type = 'submit' value = "Add student"/> </td> </tr> </table> </form> </body> </html>
Step 5 − Add the following lines in app/Http/routes.php.
app/Http/routes.php
Route::get('insert','StudInsertController@insertform'); Route::post('create','StudInsertController@insert');
Step 6 − Visit the following URL to insert record in database.
http://localhost:8000/insert
Step 7 − The output will appear as shown in the following image.
