如何在 Laravel 中將原始資料插入 MySQL 資料庫?


您可以使用 Query Builder 工具將原始資料插入 MySQL 表中。您必須包含 **類:Illuminate\Support\Facades\DB**;或使用 DB;

假設我們使用以下所示的 CREATE 語句建立了一個名為 **students** 的表 -

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 );

假設我們已使用以下資料填充了上述表格 -

+----+---------------+------------------+---------------------+---------------------+---------+------+
| 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   |
+----+---------------+------------------+---------------------+---------------------+---------+------+

示例 1

使用 insert() 方法

insert() 方法將在給定的表中新增一條記錄。它以鍵/值對形式的陣列作為輸入,其中鍵是列名,值是要為該列賦予的值。程式碼如下 -

DB::table('students')->insert([ 'name' => 'Niya Sethi', 'email' => 'niya@gmail.com', 'age'=>'20', 'address'=>'Mumbai' ]);

以上程式碼片段將以下行新增到 **students** 表中。

11, 'Niya Sethi', 'niya@gmail.com', 'Mumbai', 20

示例 2

使用 DB facade insert() 方法,您可以插入多條記錄,如下所示 -

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'] ]);

完整程式碼如下 -

<?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'] ]); } }

以上程式碼片段將以下行新增到 **students** 表中 -

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

示例 3

我們還可以使用原始插入值到表中。程式碼如下 -

DB::insert('insert into students (name, email, age,address) values (?, ?, ?, ?)',
   ['Niyati', 'niyati@gmail.com', 19, 'Pune']);

以下是將原始值插入表的完整示例 -

<?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']); } }

輸出

以上程式碼片段將以下行新增到 **students** 表中

12, 'Niyati', 'niyati@gmail.com', 'Pune', 19

示例 4

我們可以使用 eloquent 模型 student 將資料插入表中。eloquent 模型是為每個表建立的唯一類,並且對於與表相關的所有查詢,都使用與該表關聯的模型類。

程式碼如下 -

$student = new Student; $student->name = 'Amar'; $student->email = 'amar@gmail.com'; $student->age = 25; $student->address = 'Lucknow'; $student->save();

以下示例將原始資料插入 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(); } }

輸出

執行以上程式碼後,以下行將新增到 **students** 表中 -

13, 'Amar', 'amar@gmail.com', 'Lucknow', 25

最後,如果您在 MySQL 中驗證表,您可以看到所有記錄,如下所示 -

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)

更新於: 2022-08-30

6K+ 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告