diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index a8e86d5..e8cc71a 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -20,6 +20,11 @@ class UserController extends Controller abort(403); } + // not sure if this is optimal. Some way to do $user->with(...) ??? + $user = User::where('id', $user->id) + ->with(['followers', 'following']) + ->firstOrFail(); + return view('user.show', compact('user')); } } diff --git a/app/Models/User.php b/app/Models/User.php index 157248b..f8f22c9 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -50,6 +50,22 @@ class User extends Authenticatable implements MustVerifyEmail return $this->belongsToMany(Mod::class); } + /** + * The relationship between a user and users they follow + */ + public function following(): BelongsToMany + { + return $this->belongsToMany(User::class, 'user_follows', 'follower_id', 'following_id'); + } + + /** + * The relationship between a user and users that follow them + */ + public function followers(): BelongsToMany + { + return $this->belongsToMany(User::class, 'user_follows', 'following_id', 'follower_id'); + } + /** * The data that is searchable by Scout. */ diff --git a/database/migrations/2024_08_28_141058_user_follows.php b/database/migrations/2024_08_28_141058_user_follows.php new file mode 100644 index 0000000..7b7fca3 --- /dev/null +++ b/database/migrations/2024_08_28_141058_user_follows.php @@ -0,0 +1,31 @@ +id(); + $table->unsignedBigInteger('follower_id'); + $table->unsignedBigInteger('following_id'); + $table->foreign('follower_id')->references('id')->on('users')->cascadeOnDelete()->cascadeOnUpdate(); + $table->foreign('following_id')->references('id')->on('users')->cascadeOnDelete()->cascadeOnUpdate(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('user_follows'); + } +}; diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index ae97eca..04b1591 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -31,29 +31,48 @@ class DatabaseSeeder extends Seeder // Create some code licenses. $licenses = License::factory(10)->create(); - // Add 5 administrators. - $administrator = UserRole::factory()->administrator()->create(); - User::factory()->for($administrator, 'role')->create([ + // Add administrators. + $administratorRole = UserRole::factory()->administrator()->create(); + $testAccount = User::factory()->for($administratorRole, 'role')->create([ 'email' => 'test@example.com', ]); - $this->command->outputComponents()->info('test account created: test@example.com'); + $this->command->outputComponents()->info("test account created: $testAccount->email"); - User::factory(4)->for($administrator, 'role')->create(); + User::factory(4)->for($administratorRole, 'role')->create(); - // Add 10 moderators. - $moderator = UserRole::factory()->moderator()->create(); - User::factory(5)->for($moderator, 'role')->create(); + // Add moderators. + $moderatorRole = UserRole::factory()->moderator()->create(); + User::factory(5)->for($moderatorRole, 'role')->create(); - // Add 100 users. - $users = collect(progress( + // Add users + progress( label: 'adding users...', steps: $userCount, callback: fn () => User::factory()->create() - )); + ); - // Add 300 mods, assigning them to the users we just created. - $allUsers = $users->merge([$administrator, $moderator]); + // get all users + $allUsers = User::all(); + + // Add user follows + progress( + label: 'adding user follows ...', + steps: $allUsers, + callback: function ($user) use ($allUsers) { + $hasFollowers = rand(0, 100) < 70; // 70% chance to have followers + $isFollowing = rand(0, 100) < 70; // 70% chance to be following other users + + if ($hasFollowers) { + $followers = $allUsers->random(rand(1, 10))->pluck('id')->toArray(); + $user->followers()->attach($followers); + } + + if ($isFollowing) { + $following = $allUsers->random(rand(1, 10))->pluck('id')->toArray(); + $user->following()->attach($following); + } + }); $mods = collect(progress( label: 'adding mods...', @@ -71,7 +90,7 @@ class DatabaseSeeder extends Seeder } ); - // Add 3000 mod versions, assigning them to the mods we just created. + // Add mod versions, assigning them to the mods we just created. $modVersions = collect(progress( label: 'adding mods versions ...', steps: $modVersionCount,