卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章64334本站已运行4115

如何在Laravel中将原始数据插入MySQL数据库?

如何在Laravel中将原始数据插入MySQL数据库?

您可以使用查询生成器工具在 MySQL 表中插入原始数据。您必须包含类:IlluminateSupportFacadesDB;或使用数据库;

假设我们使用 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 AppHttpControllers;
use IlluminateHttpRequest;
use IlluminateSupportFacadesDB;

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 AppHttpControllers;
use IlluminateHttpRequest;
use IlluminateSupportFacadesDB;

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 AppHttpControllers;
use IlluminateHttpRequest;
use AppModelsStudent;

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)
卓越飞翔博客
上一篇: 探索写作技巧:如何撰写一份引人注目的PHP程序员求职简历
下一篇: 如何利用Python构建智能语音助手
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏