如何在 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 | [email protected] | 2022-05-01 13:45:55 | 2022-05-01 13:45:55 | xyz | 20 | | 2 | Rehan Khan | [email protected] | 2022-05-01 13:49:50 | 2022-05-01 13:49:50 | xyz | 18 | | 3 | Rehan Khan | [email protected] | NULL | NULL | testing | 20 | | 4 | Rehan | [email protected] | NULL | 2022-05-29 14:17:02 | abcd | 50 | | 5 | Nidhi Agarwal | [email protected] | NULL | NULL | abcd | 20 | | 6 | Ashvik Khanna | [email protected] | NULL | NULL | oooo | 16 | | 7 | Viraj Desai | [email protected] | NULL | NULL | test | 18 | | 8 | Priya Singh | [email protected] | NULL | NULL | test123 | 20 | | 9 | Arbaaz | [email protected] | 2022-05-29 14:11:09 | 2022-05-29 14:11:09 | testing | 35 | | 10 |Niketan Vaahi |[email protected] | NULL | NULL | testing | 35 | +----+---------------+------------------+---------------------+---------------------+---------+------+
示例 1
使用 insert() 方法
insert() 方法将在给定的表中添加一条记录。它以键/值对形式的数组作为输入,其中键是列名,值是要为该列赋予的值。代码如下 -
DB::table('students')->insert([ 'name' => 'Niya Sethi', 'email' => '[email protected]', 'age'=>'20', 'address'=>'Mumbai' ]);
以上代码片段将以下行添加到 **students** 表中。
11, 'Niya Sethi', '[email protected]', 'Mumbai', 20
示例 2
使用 DB facade insert() 方法,您可以插入多条记录,如下所示 -
DB::table('students')->insert([ ['name' => 'Peter', 'email' => '[email protected]', 'age'=>'20', 'address'=>'Chicago'], ['name' => 'David', 'email' => '[email protected]', 'age'=>'20', 'address'=>'London'], ['name' => 'Niraj', 'email' => '[email protected]', 'age'=>'20', 'address'=>'Mumbai'], ['name' => 'Sumit', 'email' => '[email protected]', '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' => '[email protected]', 'age'=>'20', 'address'=>'Chicago'], ['name' => 'David', 'email' => '[email protected]', 'age'=>'20', 'address'=>'London'], ['name' => 'Niraj', 'email' => '[email protected]', 'age'=>'20', 'address'=>'Mumbai'], ['name' => 'Sumit', 'email' => '[email protected]', 'age'=>'20', 'address'=>'Kerala'] ]); } }
以上代码片段将以下行添加到 **students** 表中 -
14, 'Peter', '[email protected]', 'Chicago', 20 15, 'David', '[email protected]', 'London', 20 16, 'Niraj', '[email protected]', 'Mumbai', 20 17, 'Sumit', '[email protected]', 'Kerala', 20
示例 3
我们还可以使用原始插入值到表中。代码如下 -
DB::insert('insert into students (name, email, age,address) values (?, ?, ?, ?)', ['Niyati', '[email protected]', 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', '[email protected]', 19, 'Pune']); } }
输出
以上代码片段将以下行添加到 **students** 表中
12, 'Niyati', '[email protected]', 'Pune', 19
示例 4
我们可以使用 eloquent 模型 student 将数据插入表中。eloquent 模型是为每个表创建的唯一类,并且对于与表相关的所有查询,都使用与该表关联的模型类。
代码如下 -
$student = new Student; $student->name = 'Amar'; $student->email = '[email protected]'; $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 = '[email protected]'; $student->age = 25; $student->address = 'Lucknow'; $student->save(); } }
输出
执行以上代码后,以下行将添加到 **students** 表中 -
13, 'Amar', '[email protected]', 'Lucknow', 25
最后,如果您在 MySQL 中验证表,您可以看到所有记录,如下所示 -
mysql> select * from students; +----+---------------+-------------------+---------------------+---------------------+---------+------+ | id | name | email | created_at | updated_at | address | age | +----+---------------+-------------------+---------------------+---------------------+---------+------+ | 1 | Siya Khan | [email protected] | 2022-05-01 13:45:55 | 2022-05-01 13:45:55 | Xyz | 20 | | 2 | Rehan Khan | [email protected] | 2022-05-01 13:49:50 | 2022-05-01 13:49:50 | Xyz | 18 | | 3 | Rehan Khan | [email protected] | NULL | NULL | testing | 20 | | 4 | Rehan | [email protected] | NULL | NULL | abcd | 15 | | 5 | Nidhi Agarwal | [email protected] | NULL | NULL | abcd | 20 | | 6 | Ashvik Khanna | [email protected] | NULL | NULL | oooo | 16 | | 7 | Viraj Desai | [email protected] | NULL | NULL | test | 18 | | 8 | Priya Singh | [email protected] | NULL | NULL | test123 | 20 | | 9 | Arbaaz | [email protected] | 2022-05-29 14:11:09 | 2022-05-29 14:11:09 | testing | 35 | | 10 | Niketan Vaahi | [email protected] | NULL | NULL | testing | 35 | | 11 | Niya Sethi | [email protected] | NULL | NULL | Mumbai | 20 | | 12 | Niyati | [email protected] | NULL | NULL | Pune | 19 | | 13 | Amar | [email protected] | NULL | NULL | Lucknow | 25 | | 14 | Peter | [email protected] | NULL | NULL | Chicago | 20 | | 15 | David | [email protected] | NULL | NULL | London | 20 | | 16 | Niraj | [email protected] | NULL | NULL | Mumbai | 20 | | 17 | Sumit | [email protected] | NULL | NULL | Kerala | 20 | +----+---------------+-------------------+---------------------+---------------------+---------+------+ 17 rows in set (0.00 sec)
广告