從零構建 Laravel 論壇二:搭建舞臺

木木林發表於2021-11-16

一、 新建專案

  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 協議》,轉載必須註明作者和本文連結

相關文章