1.初始化数据库
- 本系列文章为
laracasts.com
的系列视频教程——Let's Build A Forum with Laravel and TDD 的学习笔记。若喜欢该系列视频,可去该网站订阅后下载该系列视频, 支持正版 ;- 视频源码地址:github.com/laracasts/Lets-Build-a-...;
- 本项目为一个 forum(论坛)项目,与本站的第二本实战教程 《Laravel 教程 - Web 开发实战进阶》 类似,可互相参照。
本节说明
- 对应视频教程第 1 小节:Initial Database Setup With Seeding
本节内容
开发环境
- 开发环境与本站推荐开发环境统一,详见:Laravel 开发环境部署
- 编辑器选用 PhpStorm
新建项目
首先开启虚拟机:
> cd ~/Homestead && vagrant up
> vagrant ssh
新建一个名为 forum 的项目:
$ cd ~/Code
$ composer create-project laravel/laravel forum --prefer-dist "5.4.*"
构建模型
在本项目中,最基本的模型为 Thread , Reply , User :
# forum
1.Thread
2.Reply
3.User
A.Thread is created by a user
B.A reply belongs to a thread,and belongs to a user.
建立Thread
模型、迁移文件与控制器:
$ php artisan make:model Thread -mr
会同时生成Thread.php
模型文件,ThreadController.php
控制器文件,{timestamp}_create_threads_table.php
迁移文件。
注:该命令生成控制器时,应修改为复数形式,如 app\Http\Controllers\ThreadsController.php
修改app\Thread.php
文件:
.
.
class Thread extends Model
{
protected $guarded = []; // 意味所有属性均可更新,后期会修复此安全隐患
.
.
修改database\migrations\{timestamp}_create_threads_table.php
文件:
.
.
public function up()
{
Schema::create('threads', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->string('title');
$table->text('body');
$table->timestamps();
});
}
.
.
修改.env
文件:
APP_NAME=forum
.
.
APP_URL=http://forum.test
.
.
DB_DATABASE=forum
.
.
建立forum
数据库,并运行迁移:
$ php artisan migrate
建立Reply
模型、迁移文件与控制器:
$ php artisan make:model Reply -mr
修改app\Reply.php
文件:
.
.
class Reply extends Model
{
protected $guarded = [];
.
.
修改database\migrations\{timestamp}_create_replies_table.php
文件
.
.
public function up()
{
Schema::create('replies', function (Blueprint $table) {
$table->increments('id');
$table->integer('thread_id');
$table->integer('user_id');
$table->text('body');
$table->timestamps();
});
}
.
.
再次运行迁移:
$ php artisan migrate
模型工厂
修改database\factories\ModelFactory.php
如下:
<?php
/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| Here you may define all of your model factories. Model factories give
| you a convenient way to create models for testing and seeding your
| database. Just tell the factory how a default model should look.
|
*/
/** @var \Illuminate\Database\Eloquent\Factory $factory */
$factory->define(App\User::class, function (Faker\Generator $faker) {
static $password;
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'password' => $password ?: $password = bcrypt('123456'),
'remember_token' => str_random(10),
];
});
$factory->define(App\Thread::class,function ($faker){
return [
'user_id' => function () {
return factory('App\User')->create()->id;
},
'title' => $faker->sentence,
'body' => $faker->paragraph,
];
});
$factory->define(App\Reply::class,function ($faker){
return [
'thread_id' => function () {
return factory('App\Thread')->create()->id;
},
'user_id' => function () {
return factory('App\User')->create()->id;
},
'body' => $faker->paragraph,
];
});
数据填充
进入tinker
环境:
$ php artisan tinker
执行以下语句,填充假数据:
>>> factory('App\Reply',50)->create();