Menu

1.初始化数据库

本节说明

  • 对应视频教程第 1 小节:Initial Database Setup With Seeding

本节内容

开发环境

新建项目

首先开启虚拟机:

> 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

执行以下语句,填充假数据:

>>> $threads = factory('App\Reply',50)->create();

本文章首发在 Laravel China 社区
上一篇 下一篇
讨论数量: 0
发起讨论


暂无话题~
刻意练习,每日精进。
16
点赞
2337
浏览
0
讨论

维护者
14
14