Laravel --- artisan创建表以及填充表数据流程总结
1.创建建表文件
php artisan make:migration create_comments_table
打开database/migrations/xxx_create_comments_table.php:
public function up()
{
Schema::create('comments',function(Blueprint $table){
$table->engine = 'InnoDB';
$table->increments('id');
$table->integer('article_id');
$table->integer('user_id');
$table->string('content');
$table->timestamps();
});
} /**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('comments');
}
2.生成表
php artisan migrate
3.创建填充表数据的文件
php artisan make:seed ReplyTableSeeder
1).打开:database/seeds/CommentsTableSeeder.php
use Illuminate\Database\Seeder; class CommentsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
factory(\App\Models\Comment::class)->times(30)->create(); // 表示创建30条数据。factory方法对应第三步
}
}
2).打开database\seeds\DatabaseSeeder.php
use Illuminate\Database\Seeder; class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$this->call(CommentsTableSeeder::class); // 会调用CommentsTableSeeder的run方法
}
}
3).打开database\factories\ModelFactory.php
$factory->define(App\Models\Comment::class, function (Faker\Generator $faker) {
$user = DB::table('users')->select('id')->orderBy(DB::raw('RAND()'))->first();
if(empty($user))
{
$user->id = 0;
}
$article = DB::table('articles')->select('id')->orderBy(DB::raw('RAND()'))->first();
if(empty($article))
{
$article->id = 0;
}
return [
'user_id'=>$user->id, // user表随机查询
'article_id'=>$article->id, // 从article表u随机查询
'content' => '内容:'.$faker->text, // faker用法自寻,或转到vendor\fzaninotto\faker\src\Faker\Generator.php,看文件开头的注释
];
});
4).如何让faker填充中文
打开app\Providers\AppServiceProvider.php:
public function boot()
{
\Carbon\Carbon::setLocale('zh'); // 针对时间包,转化后为中文的时间 //生成中文数据
$this->app->singleton(FakerGenerator::class, function() {
return FakerFactory::create('zh_CN');
});
}
注:设置后faker属性仍是英文,是因为包里面就没有中文数据
4.生成数据
php artisan db:seed
5.假如要新增表,那么在建好建表的文件后,执行php artisan migrate,会提示xxx表已经存在,需要先回滚
php artisan migrate:rollback // 会调用建表文件中的down方法,把数据库结构恢复到最初状态
6.回滚完毕后,再重复1~4
Laravel --- artisan创建表以及填充表数据流程总结的更多相关文章
- laravel使用创建的request作为表单验证类
1.使用命令行工具创建request php artisan make request:validateLoginRequest 2.创建后进入app/Http/Requests目录下找到创建的文件 ...
- (转载)EF 使用code first模式创建数据库和 填充种子数据
第一篇:来自 .net 开发菜鸟 博主的文章:https://www.cnblogs.com/dotnet261010/p/8035213.html 第二篇:来自 JustYong 博主的文章:htt ...
- JavaScript之表格操作(二)创建表格病填充表格数据
//创建表格 var tableOptions = { way: "insertBefore", //insertBefore,append positionId: "d ...
- 数据库(一)--通过django创建数据库表并填充数据
django是不能创建数据库的,只能够创建数据库表,因此,我们在连接数据库的时候要先建立一个数据库. 在models.py中 from django.db import models class Pu ...
- Oracle创建删除用户,角色,表空间,导入导出数据库命令总结(转载)
无意间看到一篇文章,觉得对于ORACLE的新手很实用,特转载,原文出处这里 说明:在创建数据库时输入的密码,是修改系统默认的密码,以system和sysman等系统默认身份登录时要输入的密码就是修改后 ...
- 4.windows和Linux下创建oracleusername表空间,表,插入数据,用户管理表等操作
进入超级管理员,运行下面命令 Window下创建数据库.表空间,用户,插入数据等操作 -- 01 创建表空间 -- 注意表空间的路径 依据实际安装环境进行调整 CREATE TABLESPACE ts ...
- 在mysql数据库中创建oracle scott用户的四个表及插入初始化数据
在mysql数据库中创建oracle scott用户的四个表及插入初始化数据 /* 功能:创建 scott 数据库中的 dept 表 */ create table dept( deptno int ...
- 一起学Hive——创建内部表、外部表、分区表和分桶表及导入数据
Hive本身并不存储数据,而是将数据存储在Hadoop的HDFS中,表名对应HDFS中的目录/文件.根据数据的不同存储方式,将Hive表分为外部表.内部表.分区表和分桶表四种数据模型.每种数据模型各有 ...
- python pynssql创建表,删除表,插入数据,查询
import pymssql server='10.194.**.***:*****' user='sa' password='******' database='******' #连接 conn=p ...
随机推荐
- vue router 传递参数
vue-router 传参的方式 query 和params query 类似于get请求的传参方法 就是 ? 这种形式的 https://i.cnblogs.com/EditPosts.aspx?o ...
- phpcms视图查询数据
{pc:get sql="SELECT * FROM phpcms WHERE id in ($id) ORDER BY listorder ASC LIMIT 0, 1--"re ...
- Websphere设备、企业部署应用程序 【应用】
Websphere设备.企业部署应用实例 环境 名称 版本号 Linux系统 CentOS-5.6-x86_64 Oracle软件 10201_database_linux_x86_64.cpio W ...
- 道量化交易程序猿(25)--Cointrader之MarketData市场数据实体(12)
转载注明出处:http://blog.csdn.net/minimicall.http://cloudtrade.top/ 前面一节我们说到了远端事件.当中.市场数据就属于远端事件.市场数据有什么?我 ...
- 绝对和相对误差(absolute & relative error)
1. 标量 真实值为 x,测量值为 x0, 绝对误差(absolute error):Δx=x0−x(有单位): 相对误差(relative error):δx=Δxx=x0−xx=x0x−1(是一个 ...
- 国家模式c++
状态模式(State Pattern)是设计模式的一种,属于行为模式. 定义(源于Design Pattern):当一个对象的内在状态改变时同意改变其行为,这个对象看起来像是改变了其类. 状态模式主要 ...
- WPF 获取鼠标屏幕位置、窗口位置、控件位置
原文:WPF 获取鼠标屏幕位置.窗口位置.控件位置 public struct POINT { public int X; public int Y; public POINT(int x, int ...
- IOS开发之KVC KVO KVB
KVC(Key Value Coding) KVO(Key Value Observing) KVB(Key Value Binding) KVO是Cocoa的一个重要机制,他提供了观察某一属性变化的 ...
- WPF:如何为程序添加splashScreen?
原文:WPF:如何为程序添加splashScreen? 大家是否还记得在Windows Forms程序中如何实现splashScreen吗?我们一般都会使用Microsoft.VisualBasic. ...
- vagrant up 无法加载映像目录
错误代码显示: ==> default: Attempting graceful shutdown of VM... ==> default: Clearing any previousl ...