您可以使用查询生成器工具在 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 | [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' ]);
11, 'Niya Sethi', '[email protected]', 'Mumbai', 20
示例 2
使用数据库门面 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'] ]); } }
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']); } }
输出
12, 'Niyati', '[email protected]', 'Pune', 19
示例 4
我们可以利用一个雄辩的模范学生,将数据插入到表中。雄辩模型是为每个表创建的唯一类,对于与该表相关的所有查询,都使用与该表关联的模型类。
其代码是 -
$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(); } }
输出
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)
以上就是如何在Laravel中将原始数据插入MysqL数据库?的详细内容,更多请关注编程之家其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。