You can use the Query Builder tool to insert raw data into a MySQL table. You must include the class: Illuminate\Support\Facades\DB; or use a database;
Suppose we use the CREATE statement to create a table named students as shown below -
CREATE TABLE students( id INTEGER NOT NULL PRIMARY KEY, name VARCHAR(15) NOT NULL, email VARCHAR(20) NOT NULL, created_at VARCHAR(27), updated_at VARCHAR(27), address VARCHAR(30) NOT NULL, age INTEGER );
Assume we have populated the above table with the following data -
+----+---------------+------------------+---------------------+---------------------+---------+------+ | id | name | email | created_at | updated_at | address | age | +----+---------------+------------------+---------------------+---------------------+---------+------+ | 1 | Siya Khan | siya@gmail.com | 2022-05-01 13:45:55 | 2022-05-01 13:45:55 | xyz | 20 | | 2 | Rehan Khan | rehan@gmail.com | 2022-05-01 13:49:50 | 2022-05-01 13:49:50 | xyz | 18 | | 3 | Rehan Khan | rehan@gmail.com | NULL | NULL | testing | 20 | | 4 | Rehan | rehan@gmail.com | NULL | 2022-05-29 14:17:02 | abcd | 50 | | 5 | Nidhi Agarwal | nidhi@gmail.com | NULL | NULL | abcd | 20 | | 6 | Ashvik Khanna | ashvik@gmail.com | NULL | NULL | oooo | 16 | | 7 | Viraj Desai | viraj@gmail.com | NULL | NULL | test | 18 | | 8 | Priya Singh | priya@gmail.com | NULL | NULL | test123 | 20 | | 9 | Arbaaz | arbaaz@gmail.com | 2022-05-29 14:11:09 | 2022-05-29 14:11:09 | testing | 35 | | 10 |Niketan Vaahi |niketan@gmail.com | NULL | NULL | testing | 35 | +----+---------------+------------------+---------------------+---------------------+---------+------+
Use insert() method
insert() method will add a record to the given table. It takes input as an array containing data in key/value pairs, where the key is the column name and the value is the value to be assigned to the column. code show as below -
DB::table('students')->insert([ 'name' => 'Niya Sethi', 'email' => 'niya@gmail.com', 'age'=>'20', 'address'=>'Mumbai' ]);
The above code snippet adds the following lines to the students table.
11, 'Niya Sethi', 'niya@gmail.com', 'Mumbai', 20
Using the database facade insert() method, you can insert multiple records as shown below -
DB::table('students')->insert([ ['name' => 'Peter', 'email' => 'peter@gmail.com', 'age'=>'20', 'address'=>'Chicago'], ['name' => 'David', 'email' => 'david@gmail.com', 'age'=>'20', 'address'=>'London'], ['name' => 'Niraj', 'email' => 'niraj@gmail.com', 'age'=>'20', 'address'=>'Mumbai'], ['name' => 'Sumit', 'email' => 'sumit@gmail.com', 'age'=>'20', 'address'=>'Kerala'] ]);
The complete code is -
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; class StudentController extends Controller { public function index() { DB::table('students')->insert([ ['name' => 'Peter', 'email' => 'peter@gmail.com', 'age'=>'20', 'address'=>'Chicago'], ['name' => 'David', 'email' => 'david@gmail.com', 'age'=>'20', 'address'=>'London'], ['name' => 'Niraj', 'email' => 'niraj@gmail.com', 'age'=>'20', 'address'=>'Mumbai'], ['name' => 'Sumit', 'email' => 'sumit@gmail.com', 'age'=>'20', 'address'=>'Kerala'] ]); } }
The above code snippet adds the following lines to the students table -
14, 'Peter', 'peter@gmail.com', 'Chicago', 20 15, 'David', 'david@gmail.com', 'London', 20 16, 'Niraj', 'niraj@gmail.com', 'Mumbai', 20 17, 'Sumit', 'sumit@gmail.com', 'Kerala', 20
We can also use the original inserted values from the table. code show as below -
DB::insert('insert into students (name, email, age,address) values (?, ?, ?, ?)', ['Niyati', 'niyati@gmail.com', 19, 'Pune']);
The following is a complete example of inserting original values into a table -
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; class StudentController extends Controller { public function index() { DB::insert('insert into students (name, email, age,address) values (?, ?, ?, ?)', ['Niyati', 'niyati@gmail.com', 19, 'Pune']); } }
The above code snippet adds the following lines to the students table
12, 'Niyati', 'niyati@gmail.com', 'Pune', 19
We can use an eloquent model student to insert data into the table. An Eloquent model is a unique class created for each table, and the model class associated with that table is used for all queries related to that table.
The code is -
$student = new Student; $student->name = 'Amar'; $student->email = 'amar@gmail.com'; $student->age = 25; $student->address = 'Lucknow'; $student->save();
The following example inserts raw data into a table in MySQL -
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; class StudentController extends Controller { public function index() { $student = new Student; $student->name = 'Amar'; $student->email = 'amar@gmail.com'; $student->age = 25; $student->address = 'Lucknow'; $student->save(); } }
After executing the above code, the following rows will be added to the Students table -
13, 'Amar', 'amar@gmail.com', 'Lucknow', 25
Finally, if you validate the table in MySQL, you can see all the records as shown below -
mysql> select * from students; +----+---------------+-------------------+---------------------+---------------------+---------+------+ | id | name | email | created_at | updated_at | address | age | +----+---------------+-------------------+---------------------+---------------------+---------+------+ | 1 | Siya Khan | siya@gmail.com | 2022-05-01 13:45:55 | 2022-05-01 13:45:55 | Xyz | 20 | | 2 | Rehan Khan | rehan@gmail.com | 2022-05-01 13:49:50 | 2022-05-01 13:49:50 | Xyz | 18 | | 3 | Rehan Khan | rehan@gmail.com | NULL | NULL | testing | 20 | | 4 | Rehan | rehan@gmail.com | NULL | NULL | abcd | 15 | | 5 | Nidhi Agarwal | nidhi@gmail.com | NULL | NULL | abcd | 20 | | 6 | Ashvik Khanna | ashvik@gmail.com | NULL | NULL | oooo | 16 | | 7 | Viraj Desai | viraj@gmail.com | NULL | NULL | test | 18 | | 8 | Priya Singh | priya@gmail.com | NULL | NULL | test123 | 20 | | 9 | Arbaaz | arbaaz@gmail.com | 2022-05-29 14:11:09 | 2022-05-29 14:11:09 | testing | 35 | | 10 | Niketan Vaahi | niketan@gmail.com | NULL | NULL | testing | 35 | | 11 | Niya Sethi | niya@gmail.com | NULL | NULL | Mumbai | 20 | | 12 | Niyati | niyati@gmail.com | NULL | NULL | Pune | 19 | | 13 | Amar | amar@gmail.com | NULL | NULL | Lucknow | 25 | | 14 | Peter | peter@gmail.com | NULL | NULL | Chicago | 20 | | 15 | David | david@gmail.com | NULL | NULL | London | 20 | | 16 | Niraj | niraj@gmail.com | NULL | NULL | Mumbai | 20 | | 17 | Sumit | sumit@gmail.com | NULL | NULL | Kerala | 20 | +----+---------------+-------------------+---------------------+---------------------+---------+------+ 17 rows in set (0.00 sec)
The above is the detailed content of How to insert raw data into MySQL database in Laravel?. For more information, please follow other related articles on the PHP Chinese website!