从零构建 Laravel 论坛二:搭建舞台

一、 新建项目

  1. 创建项目
    composer create-project laravel/laravel forum --prefer-dist "8.6.6"

  2. 引入 Laravel Breeze 用户认证包

    composer require laravel/breeze
    php artisan breeze:install 
    npm  install  
    npm run dev 
    php artisan migrate

二、 创建基础表

本项目中,最基础的模型为 Topic (话题)、 Comment (评论) 、 User (用户)
User 表 Laravel 自带先不做修改

2.1、 新建 Topic

  1. 创建 Topic 相关文件
    php artisan make:model Topic -a

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

  2. 修改 Migration 文件

    . 
    . 
    public function up() { 
     Schema::create('topics', function (Blueprint $table) { 
         $table->id();
         $table->integer('user_id'); 
         $table->string('title'); 
         $table->longText('body'); 
         $table->timestamps(); 
     }); 
    } 
    .
    .
  3. 运行迁移
    php artisan migrate

  4. 修改 Model 文件

     .
     .
     protected $fillable = ['user_id', 'title', 'body'];
    
     public function user()
     {
         return $this->belongsTo(User::class);
     }
     .
     .
  5. 修改 Factory 文件

     .
     .
     use App\Models\User;
     .
     .
     public function definition()
     {
         return [
             'user_id' => User::factory(),
             'title' => $this->faker->sentence,
             'body' => $this->faker->paragraph,
         ];
     }
     .
     .
  6. 修改 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
                 ]);
         }
     }
     .
     .

2.2、 新建 Comment

  1. 创建 Comment 相关文件
    php artisan make:model Comment -a

  2. 修改 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();
     }); 
    } 
    .
    .
  3. 运行迁移
    php artisan migrate

  4. 修改 Model 文件

     .
     .
     protected $fillable = ['user_id', 'topic_id', 'body'];
    
     public function user()
     {
         return $this->belongsTo(User::class);
     }
    
     public function topic()
     {
         return $this->belongsTo(Topic::class);
     }
     .
     .
  5. 修改 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,
         ];
     }
     .
     .
  6. 修改 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, 3))->get();
             foreach ($users as $user) {
                 Comment::factory()
                     ->count(mt_rand(0, 3))
                     ->create([
                         'user_id' => $user->id,
                         'topic_id' => $topic->id,
                     ]);
             }
         }
     }
     .
     .

2.3、 填充测试数据

  1. 修改 DatabaseSeeder 文件

     public function run()
     {
         User::factory(10)->create();
         $this->call([
             TopicSeeder::class,
             CommentSeeder::class,
         ]);
     }
  2. 运行填充
    php artisan db:seed

本作品采用《CC 协议》,转载必须注明作者和本文链接
木木林
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 1

好,加油,别再懒了哈!

2年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!