首頁 >後端開發 >php教程 >如何在Laravel中將原始資料插入MySQL資料庫?

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

PHPz
PHPz轉載
2023-09-09 16:09:051011瀏覽

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

您可以使用查詢產生器工具在 MySQL 表中插入原始資料。您必須包含類別:Illuminate\Support\Facades\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

使用資料庫門面 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

我們可以利用一個雄辯的模範學生,將資料插入表中。雄辯模型是為每個表建立的唯一類,對於與該表相關的所有查詢,都使用與該表關聯的模型類。

其程式碼是 -

$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();
   }
}

輸出

執行上述程式碼後,以下行將會加入學生表格中 -

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)

以上是如何在Laravel中將原始資料插入MySQL資料庫?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除