Insert a record using Laravel Query

syntax for insert record

Syntaxbool insert(string $query, array $bindings = array())
Parameters
  • $query(string) − query to execute in database
  • $bindings(array) − values to bind with queries
Returnsbool
DescriptionRun an insert statement against the database.

Simple Example To insert data


Step 1 − Execute the below command to create a controller called StudInsertController

php artisan make:controller StudInsertController --plain

Step 2 − after step1 completed then, you will receive the following output like

Step 3 − Then Copy the following code to file

app/Http/Controllers/StudInsertController.php

app/Http/Controllers/StudInsertController.php

input('stud_name');
      DB::insert('insert into student (name) values(?)',[$name]);
      echo "Record inserted successfully.
"; echo 'Click Here to go back.'; } }

Step 4 − Create a view file in the Following Location resources/views/stud_create.php and copy the following code in that file.

resources/views/stud_create.php


   
      Student Management | Add
   

   
      
Name

Step 5 − Add the following lines in app/Http/routes.php.

app/Http/routes.php

Route::get('insert','[email protected]');
Route::post('create','[email protected]');

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.