laravel 使用测试驱动开发一个论坛系列
请忽略blog这个命名,blog是我开发域名,不管什么项目
我用docker开发环境加phpstorm, 源码见相关的github地址
启动docker环境
$ docker-compose up -d
Creating network "dnmp_net-php72" with the default driver
Creating network "dnmp_net-mysql" with the default driver
Creating network "dnmp_net-redis" with the default driver
Creating dnmp_nginx_1 ... done
Creating dnmp_phpredisadmin_1 ... done
Creating dnmp_mysql_1 ... done
Creating dnmp_redis_1 ... done
Creating dnmp_php72_1 ... done
Creating dnmp_phpmyadmin_1 ... done
进入docker 容器
$winpty docker-compose exec php72 zsh
初始化框架
$composer create-project --prefer-dist laravel/laravel blog.dock.com -vvv
写相关的产品说明
# 论坛
1.帖子
2.回复
3.用户
A.一个帖子是由一个用户创建的
B.一个回复属于一个帖子并且属于一个用户
开始码码
添加thread(帖子)
➜ html cd blog.dock.com
➜ blog.dock.com php artisan make:model App\\Models\\Thread -a
Model created successfully.
Factory created successfully.
Created Migration: 2018_10_31_003004_create_threads_table
Controller created successfully.
thread迁移文件
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateThreadsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('threads', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->string('title');
$table->text('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('threads');
}
}
生成回复模型 reply
➜ blog.dock.com php artisan make:model App\\Models\\Reply -a
Model created successfully.
Factory created successfully.
Created Migration: 2018_10_31_003725_create_replies_table
Controller created successfully.
➜ blog.dock.com
回复迁移文件
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateRepliesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('replies', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('thread_id');
$table->text('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('replies');
}
}
更新生成工厂 reply
<?php
use Faker\Generator as Faker;
$factory->define(App\Models\Reply::class, function (Faker $faker) {
return [
//
'user_id' => function () {
return factory(\App\User::class)->create()->id;
},
'thread_id' => function () {
return factory(\App\Models\Thread::class)->create()->id;
},
'body' => $faker->paragraph,
];
});
更改生成工厂 thread
<?php
use Faker\Generator as Faker;
$factory->define(App\Models\Thread::class, function (Faker $faker) {
return [
'user_id' => function () {
return factory(\App\User::class)->create()->id;
},
'title' => $faker->sentence,
'body' => $faker->paragraph
];
});
生成调用
➜ blog.dock.com php artisan make:seeder ThreadSeeder
Seeder created successfully.
➜ blog.dock.com
相关的代码
<?php
use Illuminate\Database\Seeder;
class ThreadSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//
return factory(\App\Models\Thread::class, 50)->create()
->each(function($thread){
return factory(\App\Models\Reply::class, 10)->create(['thread_id'=> $thread->id]);
});
}
}
更改数据库配置文件 .env文件中修改如下,并建相关的数据库
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=blog
DB_USERNAME=root
DB_PASSWORD=123456
执行迁移
php artisan migrate
生成数据
php artisan db:seed --class ThreadSeeder