Laravel5.1学习笔记18 数据库4 数据填充
#简介
Laravel includes a simple method of seeding your database with test data using seed classes. All seed classes are stored in database/seeds. Seed classes may have any name you wish, but probably should follow some sensible convention, such as UserTableSeeder, etc. By default, a DatabaseSeeder class is defined for you. From this class, you may use the call method to run other seed classes, allowing you to control the seeding order.
Laravel 引入了一个简单方法用测试数据来填充你的数据库, 所有的数据填充类(seed classes)都放置在 database/seeds路径下, 数据库填充类可以有任何你希望的名字, 但是最好有一定的规范,比如UserTableSeeder, 等。默认, 一个 DatabaseSeeder 类给你定义好,从这个类,你可以使用call方法来运行其他填充类, 允许你控制填充的顺序。
#编写数据填充类
To generate a seeder, you may issue the make:seeder Artisan command. All seeders generated by the framework will be placed in the database/seeders directory:
要生成一个填充类你可以执行一个 make : seeder Artisan 命令。 所有的框架生成的填充类都放置在 database/seeders目录下
php artisan make:seeder UserTableSeeder
A seeder class only contains one method by default: run. This method is called when the db:seed Artisan commandis executed. Within the run method, you may insert data into your database however you wish. You may use thequery builder to manually insert data or you may use Eloquent model factories.
As an example, let's modify the DatabaseSeeder class which is included with a default installation of Laravel. Let's add a database insert statement to the run method:
一个填充类seeder 默认只包含一个run方法, 当执行db: seed Artisan 命令的时候就会被执行。 这个run方法里面,你如愿可以插入数据到数据库,你可以使用查询构造器手动插入数据,或使用 Eloquent 模型工厂。
作为一个例子,让我们Laravel修改附带安装的DatabaseSeeder类,我们添加数据库插入语句到run方法。
<?php use DB;
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model; class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('users')->insert([
'name' => str_random(10),
'email' => str_random(10).'@gmail.com',
'password' => bcrypt('secret'),
]);
}
}
使用模型工厂
Of course, manually specifying the attributes for each model seed is cumbersome. Instead, you can use model factories to conveniently generate large amounts of database records. First, review the model factory documentation to learn how to define your factories. Once you have defined your factories, you may use the factoryhelper function to insert records into your database.
For example, let's create 50 users and attach a relationship to each user:
当然,手动为每个模型填充指定属性比较累赘,相反,你可以使用模型工厂方便地产生大量的数据记录。首先,先看看模型工厂文档来学习怎么样定义你的工厂,一旦你定义了工厂,你就可以使用工厂辅助类方法去插入数据到数据库。
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
factory('App\User', 50)->create()->each(function($u) {
$u->posts()->save(factory('App\Post')->make());
});
}
调用额外的填充类
Within the DatabaseSeeder class, you may use the call method to execute additional seed classes. Using the callmethod allows you to break up your database seeding into multiple files so that no single seeder class becomes overwhelmingly large. Simply pass the name of the seeder class you wish to run:
在DatabaseSeeder类中你可以使用call方法去执行额外的填充类,使用call方法允许你打散你的数据库填充到多个文件,这样单个填充类不会过于庞大,只需简单的把填充类的名字传给call方法
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Model::unguard(); $this->call('UserTableSeeder');
$this->call('PostsTableSeeder');
$this->call('CommentsTableSeeder');
}
#运行填充
Once you have written your seeder classes, you may use the db:seed Artisan command to seed your database. By default, the db:seed command runs the DatabaseSeeder class, which may be used to call other seed classes. However, you may use the --class option to specify a specific seeder class to run individually:
一旦你写好了填充类,你就可以用db: seed Artisan 命令来填充数据库,默认db: seed 命令运行DatabaseSeeder类,它用来调用其它填充类。 然而,你可以使用 --class选项来定义一个指定的填充类来单独运行:
php artisan db:seed php artisan db:seed --class=UserTableSeeder
You may also seed your database using the migrate:refresh command, which will also rollback and re-run all of your migrations. This command is useful for completely re-building your database:
你也可以使用 migrate:refresh 命令填充数据库,可以回滚和重新运行所有你的迁移。 这个命令对于重建你的数据库是十分有用的。
php artisan migrate:refresh --seed
Laravel5.1学习笔记18 数据库4 数据填充的更多相关文章
- Laravel5.1学习笔记17 数据库3 数据迁移
介绍 建立迁移文件 迁移文件结构 执行迁移 回滚迁移 填写迁移文件 创建表 重命名/ 删除表 创建字段 修改字段 删除字段 建立索引 删除索引 外键约束 #介绍 Migrations are lik ...
- Laravel5.1学习笔记15 数据库1 数据库使用入门
简介 运行原生SQL查询 监听查询事件 数据库事务 使用多数据库连接 简介 Laravel makes connecting with databases and running queries e ...
- Laravel5.1学习笔记16 数据库2 查询构造器(这个不用看,不如用EloquentORM)
Introduction Retrieving Results Aggregates Selects Joins Unions Where Clauses Advanced Where Clauses ...
- SQL反模式学习笔记18 减少SQL查询数据,避免使用一条SQL语句解决复杂问题
目标:减少SQL查询数据,避免使用一条SQL语句解决复杂问题 反模式:视图使用一步操作,单个SQL语句解决复杂问题 使用一个查询来获得所有结果的最常见后果就是产生了一个笛卡尔积.导致查询性能降低. 如 ...
- springmvc学习笔记(18)-json数据交互
springmvc学习笔记(18)-json数据交互 标签: springmvc springmvc学习笔记18-json数据交互 springmvc进行json交互 环境准备 加入json转换的依赖 ...
- Mysql数据库学习笔记之数据库索引(index)
什么是索引: SQL索引有两种,聚集索引和非聚集索引,索引主要目的是提高了SQL Server系统的性能,加快数据的查询速度与减少系统的响应时间. 聚集索引:该索引中键值的逻辑顺序决定了表中相应行的物 ...
- R学习笔记(4): 使用外部数据
来源于:R学习笔记(4): 使用外部数据 博客:心内求法 鉴于内存的非持久性和容量限制,一个有效的数据处理工具必须能够使用外部数据:能够从外部获取大量的数据,也能够将处理结果保存.R中提供了一系列的函 ...
- .NET MVC 学习笔记(六)— 数据导入
.NET MVC 学习笔记(六)—— 数据导入 在程序使用过程中,有时候需要新增大量数据,这样一条条数据去Add明显不是很友好,这时候最好就是有一个导入功能,导入所需要的数据,下面我们就一起来看一下导 ...
- MongoDB学习笔记:MongoDB 数据库的命名、设计规范
MongoDB学习笔记:MongoDB 数据库的命名.设计规范 第一部分,我们先说命名规范. 文档 设计约束 UTF-8 字符 不能包含 \0 字符(空字符),这个字符标识建的结尾 . 和 $ ...
随机推荐
- 【Codeforces 1027D】Mouse Hunt
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 先求出来强连通分量. 每个联通分量里面,显然在联通块的尽头(没有出度)放一个捕鼠夹就ok了 [代码] #include <bits/st ...
- 关于字符串不为空 错误:s!=null
错误:s!=null 正确:StringUtils.isNotBlank(s); public static boolean isBlank(CharSequence cs) { int strLen ...
- 备用交换机(cogs 8)
[问题描述] n个城市之间有通讯网络,每个城市都有通讯交换机,直接或间接与其它城市连接.因电子设备容易损坏,需给通讯点配备备用交换机.但备用交换机数量有限,不能全部配备,只能给部分重要城市配置.于是规 ...
- HDU 4849 Wow! Such City!陕西邀请赛C(最短路)
HDU 4849 Wow! Such City! 题目链接 题意:依照题目中的公式构造出临接矩阵后.求出1到2 - n最短路%M的最小值 思路:就依据题目中方法构造矩阵,然后写一个dijkstra,利 ...
- F广搜
<span style="color:#330099;">/* F - 广搜 基础 Time Limit:1000MS Memory Limit:10000KB 64b ...
- GPS时间
GPS信息里面包含一个时间戳. phonegap(即cordova)的地理位置API Geolocation 提供了对设备GPS传感器的访问,返回的数据中,除了坐标,还有一个时间戳timestamp. ...
- Codeforces Round #329 (Div. 2)B. Anton and Lines 贪心
B. Anton and Lines The teacher gave Anton a large geometry homework, but he didn't do it (as usual ...
- Bootloader - main system - Recovery的三角关系【转】
本文转载自:http://blog.csdn.net/u012719256/article/details/52304273 一.MTD分区: BOOT: boot.img,Linux ...
- 深入理解7816(3)-----关于T=0 【转】
本文转载自:http://blog.sina.com.cn/s/blog_4df8400a0102vcyp.html 深入理解7816(3)-----关于T=0 卡片和终端之间的数据传输是通过命令响应 ...
- unpe13e 学习备忘1
转其他地方. http://blog.sina.com.cn/s/blog_a43aba560101a2s5.html 运行书本中的程序.1,首先,下载源码:unpv13e.tar.gz2,然后,编 ...