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 1102E】Monotonic Renumeration
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 会发现如果a[i]=a[j] 那么b[i]~b[j]都是相同的,等于b[i] 而b[i]等于b[i-1]+1或者b[i] 有两种可能 所以对于 ...
- Leetcode 131.分割回文串
分割回文串 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab" 输出: [ ["aa" ...
- [bzoj1356]Rectangle[Baltic2009][几何常识乱搞]
虽然说是几何常识乱搞,但是想不到啊.. 题意:n个点取4个组成矩形,使面积最大,求面积. n<=1500 题解: 1.对角线相等且相互交于中点的四边形是矩形. 2.矩形四点共圆. 所以$n^2$ ...
- A + B Problem Too
Problem Description This problem is also a A + B problem,but it has a little difference,you should d ...
- H - Parity game 并查集
Now and then you play the following game with your friend. Your friend writes down a sequence consis ...
- vue2的简单Popup (Confirm,Alert)组件
github: https://github.com/longfei59418888/vui (记得给一个 start,以后有一起讨论,各种好组件) demo :http://60.205.20 ...
- SIGSEGV 和 SIGBUS & gdb看汇编
参考这篇文章: http://blog.chinaunix.net/uid-24599332-id-2122898.html SIGBUS和SIGSEGV也许是我们在平时遇到的次数最多的两个内存错误信 ...
- Unity uGui RawImage 渲染小地图
制作类似 RPG 游戏时,可能会须要显示小地图. 小地图的制作一种方式是用还有一个摄像机来渲染到一张纹理上.实时显示到UI界面. 以Unity 5.0 的 UI 系统为例: 在地图正上方放置一个摄像机 ...
- xcode,git tips
change organization name 选中project or target,最右侧Utilities面板->Project Document 修改source folder名字 - ...
- 玩转CPU之直线
近期在看编程之美,看到第一个问题时,一下子就被吸引了,原来在windows 的任务管理器中还能够让CPU舞动起来,再一次的相信了编程中仅仅有想不到没有做不到,对于书中的做法和网上的实现大致都同样.只是 ...