其他分享
首页 > 其他分享> > laravel8的Migration、Factory、Seeder

laravel8的Migration、Factory、Seeder

作者:互联网

准备工作,下载phpstudy搭建lnmp环境,搭建laravel环境,创建虚拟域名
composer create-project --prefer-dist laravel/laravel blog
记得配置解决laravel路由除了首页报错404的问题,修改php.ini和.htaccess文件,数据库完成后记得修改.env文件配置信息   migration创建表(user表直接用laravel8框架自带的)
php artisan make:migration create_blogs --create=blogs //博客表, 
php artisan make:migration create_comments --create=comments //评论表 
编辑 /database/migrations/ 下的针对 create_blogs 和 create_comments 两个文件   create_blogs文件
public function up()
{
    Schema::create('blogs', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title');
        $table->text('content');
        $table->timestamps();
    });
}

 

create_comments文件
public function up()
{
    Schema::create('comments', function (Blueprint $table) {
        $table->increments('id');
        $table->string('content');
        $table->integer('blog_id'); //这条评论是针对哪一篇博客的?
        $table->integer('user_id'); //这条评论是哪一位用户发送的?
        $table->timestamps();
    });
}

 

by the way 在我创建数据表的时候总是会给我报错说我的varchar的字符串长度怎么怎么样的报错,这是的修改方案在App\Providers\AppServiceProvider文件中修改
use Illuminate\Support\Facades\Schema;
public function boot()
{
    Schema::defaultStringLength(200);
}

 

确保你的数据库中没有blogs、comments表 php artisan migrate 这时你所需要的的数据表就建好了     使用模型工厂 Factory 来插入虚构的数据   创建模型工厂
php artisan make:factory BlogFactory --model=Blog php artisan make:factory CommentsFactory --model=Comments  
顺便生成两个模型
php artisan make:model Blog 
php artisan make:model Comments
  编辑模型工厂 /database/factories下的BlogFactory文件和CommentsFactory文件 BlogFactory文件
public function definition()
{
    return [
        'title' => $this->faker->name,
        'content' => $this->faker->text,
    ];
}

 

CommentsFactory文件
return [
    'content' => $this->faker->text,
    'blog_id' => 1,
    'user_id' => 1,
];

  

两种自动生成测试数据的方式 第一种:使用 tinker 模式调试代码 php artisan tinker 当命令提示符变为 ">>>" 时,你就处于tinker模式下了
//命令生成模拟数据,记得模型的路由一定要写正确
App\Models\User::factory()->create(); 
//生成多条数据
App\Models\User::factory()->count(100)->create();

想要生成其他表的数据,就将路由模型换一下就好了(ctrl】+【c】 可退出tinker)

  第二种:使用 Seeder 一次性完成多个数据库的批量虚拟数据插入   创建seeder
php artisan make:seeder UserTableSeeder 
php artisan make:seeder BlogTableSeeder
php artisan make:seeder CommentsTableSeeder

修改 /database/seeds/下的UserTableSeeder、BlogTableSeeder和CommentsTableSeeder文件

UserTableSeeder.php文件
use App\Models\User;
public function run()
{
    \App\Models\User::factory(50)->create(); //向users表中插入50条模拟数据

    $user = User::find(1); //插入完后,找到 id 为 1 的用户

    $user->name = "yonghu"; //设置 用户名

    $user->email = "yonghu@test.com"; //设置 邮箱

    $user->password = bcrypt('yonghu'); //设置 密码

    $user->save(); //保存
}
BlogTableSeeder.php文件
use App\Models\Blog;
public function run()
{
    \App\Models\Blog::factory(50)->create();
}

CommentsTableSeeder.php

use App\Models\Comments;
public function run()
{
    \App\Models\Comments::factory(50)->create();
}

DatabaseSeeder.php文件

public function run()
{
    // \App\Models\User::factory(10)->create();
    $this->call(UserTableSeeder::class);

    $this->call(BlogTableSeeder::class);

    $this->call(CommentsTableSeeder::class);
}

然后输入命令

php artisan migrate:refresh --seed

  

然后就会发现三张表中已经有50条模拟数据。

标签:function,laravel8,create,Factory,Seeder,artisan,table,php,App
来源: https://www.cnblogs.com/tuxuanxuan/p/14262373.html