一、下载 laravel 5.5
composer create-project laravel/laravel=5.5.* laravel5.5_factory
新建数据库 laravel5.5_factory
修改 .evn 配置文件
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel5.5_factory
DB_USERNAME=laravel5.5_factory
DB_PASSWORD=laravel5.5_factory
修改中国时区,在 config/app.php 中修改
'timezone' => 'PRC',
切换目录
cd laravel5.5_factory
二、创建模型
创建 post model
php artisan make:model Post -m
修改迁移文件:posts_table.php
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->text('content');
$table->timestamps();
});
}
执行数据迁移
php artisan migrate
如果报错
Specified key was too long; max key length is 1000 bytes
问题解决:在 AppServiceProvider 中调用 Schema::defaultStringLength 方法来实现配置:
use Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(191);
}
三、factory
创建文章 factory
php artisan make:factory PostFactory --model=Post
此命令会生成一个文件:database/factories/PostFactory.php,打开这个文件,进行修改
$factory->define(App\Post::class, function (Faker $faker) {
return [
'title' => $faker->sentence,
'content' => $faker->paragraph,
];
});
进入 tinker
php artisan tinker
执行 factory
factory('App\Post', 5)->create()