从零开始构建自己的论坛(一) —— Laravel 8.* 版

420 阅读2分钟

「这是我参与11月更文挑战的第1天,活动详情查看:2021最后一次更文挑战

前言

之前看到好的技术文章或者教程都是收藏了事,并没有认真学习里面的知识,借由这个活动从新学习,希望能坚持下去。

由于是从头开始开发调试,必然有些地方考虑不周,在之后的开发过程中如果发现前面流程错误,也会对应进行修正。

本系列文章参考Let's Build A Forum with Laravel and TDD ,原教程是基于 Laravel 5.4 版本开发, 本系列文章将以 Laravel 8 版本进行开发,也可以多了解 Laravel 版本的变迁 。若喜欢该系列视频,可去该网站订阅后下载该系列视频, 支持正版

一、新建项目

1.1 新建项目

composer create-project laravel/laravel forum --prefer-dist "8.6.6"

1.2 创建项目基础数据库

本项目中,最基础的模型为 Topic (话题)、 Comment (评论) 、 User (用户)

User 表 Laravel 自带先不做修改

1.2.1 创建 Topic 表相关文件

  1. 创建 Topic 表
php artisan make:model Topic -a 

-a 将会自动生成对应的 Model、Factory、Migration、Seeder、Controller、Policy

image.png

  1. 修改 Migration 文件
. 
. 
public function up() { 
    Schema::create('topics', function (Blueprint $table) { 
        $table->id();
        $table->integer('user_id'); 
        $table->string('title'); 
        $table->longText('body'); 
        $table->timestamps(); 
    }); 
} 
.
.
  1. 运行迁移

php artisan migrate

  1. 修改 Model 文件
    .
    .
    
    use HasFactory;

    protected $fillable = ['user_id', 'title', 'body'];

    public function user()
    {
        return $this->belongsTo(User::class);
    }
    .
    .
  1. 修改 Factory 文件
    .
    .
    use App\Models\User;
    .
    .
    public function definition()
    {
        return [
            'user_id' => User::factory(),
            'title' => $this->faker->sentence,
            'body' => $this->faker->paragraph,
        ];
    }
    .
    .
  1. 修改 Seeder 文件
    .
    .
    use App\Models\User;
    use App\Models\Topic;
    .
    .
    public function run()
    {
        $users = User::all();

        foreach ($users as $user) {
            Topic::factory()
                ->count(mt_rand(0, 10))
                ->create([
                    'user_id' => $user->id
                ]);
        }
    }
    .
    .

1.2.2 创建 Comment 表相关文件

  1. 创建 Comment 表相关文件
php artisan make:model Comment -a 
  1. 修改 Migration 文件
. 
. 
public function up() { 
    Schema::create('comments', function (Blueprint $table) { 
        $table->id();
        $table->integer('topic_id'); 
        $table->integer('user_id'); 
        $table->text('body'); 
        $table->timestamps();
    }); 
} 
.
.
  1. 运行迁移

php artisan migrate

  1. 修改 Model 文件
    .
    .
    
    use HasFactory;

    protected $fillable = ['user_id', 'topic_id', 'body'];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function topic()
    {
        return $this->belongsTo(Topic::class);
    }
    .
    .
  1. 修改 Factory 文件
    .
    .
    use App\Models\User;
    use App\Models\Topic;
    .
    .
    public function definition()
    {
        return [
            'topic_id' => Topic::factory(),
            'user_id' => User::factory(),
            'body' => $this->faker->paragraph,
        ];
    }
    .
    .
  1. 修改 Seeder 文件
    .
    .
    use App\Models\User;
    use App\Models\Topic;
    use App\Models\Comment;
    .
    .
    public function run()
    {
        $topics = Topic::all();

        foreach ($topics as $topic) {
            $users = User::inRandomOrder()->limit(mt_rand(0, 10))->get();
            foreach ($users as $user) {
                Comment::factory()
                    ->count(mt_rand(0, 10))
                    ->create([
                        'user_id' => $user->id,
                        'topic_id' => $topic->id,
                    ]);
            }
        }
    }
    .
    .

1.2.2 运行迁移

修改 DatabaseSeeder 文件

    public function run()
    {
        User::factory(10)->create();
        $this->call([
            TopicSeeder::class,
            CommentSeeder::class,
        ]);
    }

运行填充

php artisan db:seed