From ffe661799c7cde70af785dbdc4784eaca3a0655d Mon Sep 17 00:00:00 2001 From: Muizz-DS <47732301+Muizz-DS@users.noreply.github.com> Date: Thu, 7 Aug 2025 17:19:29 +0800 Subject: [PATCH] add post model and factory --- app/Models/Post.php | 16 ++++++++++ app/Models/User.php | 5 ++++ database/factories/PostFactory.php | 26 ++++++++++++++++ .../2025_08_07_083404_create_posts_table.php | 30 +++++++++++++++++++ database/seeders/UsersTableSeeder.php | 3 ++ routes/web.php | 8 +++++ 6 files changed, 88 insertions(+) create mode 100644 app/Models/Post.php create mode 100644 database/factories/PostFactory.php create mode 100644 database/migrations/2025_08_07_083404_create_posts_table.php diff --git a/app/Models/Post.php b/app/Models/Post.php new file mode 100644 index 0000000..3f04eee --- /dev/null +++ b/app/Models/Post.php @@ -0,0 +1,16 @@ +belongsTo(User::class, 'user_id'); + } +} diff --git a/app/Models/User.php b/app/Models/User.php index f7efc12..f4927e7 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -4,6 +4,7 @@ // use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; +use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Laravel\Sanctum\HasApiTokens; @@ -44,4 +45,8 @@ class User extends Authenticatable 'email_verified_at' => 'datetime', 'password' => 'hashed', ]; + + public function posts(): HasMany { + return $this->hasMany(Post::class); + } } diff --git a/database/factories/PostFactory.php b/database/factories/PostFactory.php new file mode 100644 index 0000000..916b2cf --- /dev/null +++ b/database/factories/PostFactory.php @@ -0,0 +1,26 @@ + + */ +class PostFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'title' => fake()->word(), + 'description' => fake()->text(), + 'user_id' => User::factory(), + ]; + } +} diff --git a/database/migrations/2025_08_07_083404_create_posts_table.php b/database/migrations/2025_08_07_083404_create_posts_table.php new file mode 100644 index 0000000..c25b1d8 --- /dev/null +++ b/database/migrations/2025_08_07_083404_create_posts_table.php @@ -0,0 +1,30 @@ +id(); + $table->string('title'); + $table->text('description')->nullable(); + $table->foreignId('user_id')->constrained(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('posts'); + } +}; diff --git a/database/seeders/UsersTableSeeder.php b/database/seeders/UsersTableSeeder.php index a46cf10..7f27cb1 100644 --- a/database/seeders/UsersTableSeeder.php +++ b/database/seeders/UsersTableSeeder.php @@ -2,6 +2,7 @@ namespace Database\Seeders; +use App\Models\Post; use Illuminate\Database\Console\Seeds\WithoutModelEvents; use Illuminate\Database\Seeder; use Illuminate\Support\Facades\Hash; @@ -33,5 +34,7 @@ public function run(): void $user->assignRole([$role->id]); Role::create(['name' => 'Staff']); + + Post::factory()->count(300)->create(); } } diff --git a/routes/web.php b/routes/web.php index 02a15d1..80ae424 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,5 +1,6 @@ author->name . "
"; + } +}); + Auth::routes(); Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');