Laravel 專案:使用 TDD 構建論壇 Chapter 1

洛未必達發表於2018-04-18

0.寫在前面

  • 本系列文章為laracasts.com 的系列視訊教程——Let's Build A Forum with Laravel and TDD 的學習筆記。若喜歡該系列視訊,可去該網站訂閱後下載該系列視訊, 支援正版
  • 視訊原始碼地址:https://github.com/laracasts/Lets-Build-a-Forum-in-Laravel
  • 本專案為一個 forum(論壇)專案,與本站的第二本實戰教程 Laravel 教程 - Web 開發實戰進階 ( Laravel 5.5 ) 類似,可互相參照
  • 專案開發模式為TDD開發,教程簡介為:

    A forum is a deceptively complex thing. Sure, it's made up of threads and replies, but what else might exist as part of a forum? What about profiles, or thread subscriptions, or filtering, or real-time notifications? As it turns out, a forum is the perfect project to stretch your programming muscles. In this series, we'll work together to build one with tests from A to Z.

  • 專案版本為laravel 5.4,教程後面會進行升級到laravel 5.5的教學
  • 視訊教程共計 102 個小節,筆記章節與視訊教程一一對應

1.本節說明

  • 對應視訊教程第 1 小節:Initial Database Setup With Seeding

2.本節內容

開發環境

新建專案

首先開啟虛擬機器:

> 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

會同時生成app\Thread.php模型檔案,app\Http\Controllers\ThreadController.php控制器,database\migrations\{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\Thread',50)->create()
>>> $threads = factory('App\Thread',50)->create()
>>> $threads->each(function ($thread){ factory('App\Reoly',10)->create(['thread_id' => $thread->id]);});

3.筆記心得

  • Git 相關操作略去不表
  • 自己動手寫筆記才更體會到本站實戰教程撰寫的用心,給編者大大們大大的贊!

4.寫在後面

  • 如有建議或意見,歡迎指出~
  • 如果覺得文章寫的不錯,請點贊鼓勵下哈,你的鼓勵將是我的動力!
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章