1.生成建表的 migrate 文件
php artisan make:migration create_posts_table
运行后在 database/migrations 生成 2022_05_15_063003_create_posts_table.php 编写文件如下:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->rememberToken();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->longText('introduction');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
2.创建工厂类,的同时创建model文件,在工厂类中填充数据
php artisan make:model Models/Post
php artisan make:factory PostFactory
运行后在database/factories 生成 PostFactory.php 文件 编写如下:
<?php
/** @var \Illuminate\Database\Eloquent\Factory $factory */
use App\Models\Post;
use Faker\Generator as Faker;
use Illuminate\Support\Str;
$factory->define(Post::class, function (Faker $faker) {
$date_time = $faker->date . ' ' . $faker->time;
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'remember_token' => Str::random(10),
'email_verified_at' => now(),
'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret
'introduction' => $faker->sentence(),
'created_at' => $date_time,
'updated_at' => $date_time,
];
});
3.创建Seeders
php artisan make:seeder PostsTableSeeder
在database/seeds/PostsTableSeeder.php添加如下:
<?php
use Illuminate\Database\Seeder;
class PostsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
factory(App\Models\Post::class)->times(10)->make()->each(function($post,$index){
$post->save();
});
}
}
3.配置工厂类运行
在database/seeders/DatabaseSeeder.php添加如下:
public function run()
{
// $this->call(UserSeeder::class);
$this->call(PostsTableSeeder::class);
}
4.生成表和填充数据
运行创建数据表:php artisan migrate
运行填充数据:php artisan db:seed
在执行迁移命令, 生成数据表时, 带上参数即可完成填充数据: php artisan migrate --seed
5.其他相关命令
回滚到最后一批的迁移,这可能会包含多个迁移文件:php artisan migrate:rollback
回滚你应用程序所有的迁移:php artisan migrate:reset
重新创建整个数据库:php artisan migrate:refresh
重建整个数据库并填充数据:php artisan migrate:refresh --seed
使用 —class 选项来指定一个特定的 seeder 类:php artisan db:seed --class=PostsTableSeeder
6.追加字段、修改字段和回滚
追加需要的包:composer require doctrine/dbal
选用2.0版本,默认是3.3.6 修改composer.json文件后,运行composer update
生成修改表结构的文件:php artisan make:migration add_status_to_posts_table
编码如下:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddStatusToPostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('posts', function (Blueprint $table) {
//
$table->bigInteger('status')->default(0);// 增加字段信息
$table->string('name',32)->change();// 修改原有字段属性
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('posts', function (Blueprint $table) {
//
$table->dropColumn('status');
});
}
}
运行:php artisan migrate
回滚:php artisan migrate:rollback