add user follows

This commit is contained in:
IsWaffle 2024-08-28 16:59:28 -04:00
parent 71a336ecab
commit a7cd60a164
4 changed files with 85 additions and 14 deletions

View File

@ -20,6 +20,11 @@ class UserController extends Controller
abort(403); 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')); return view('user.show', compact('user'));
} }
} }

View File

@ -50,6 +50,22 @@ class User extends Authenticatable implements MustVerifyEmail
return $this->belongsToMany(Mod::class); 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. * The data that is searchable by Scout.
*/ */

View File

@ -0,0 +1,31 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('user_follows', function (Blueprint $table) {
$table->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');
}
};

View File

@ -31,29 +31,48 @@ class DatabaseSeeder extends Seeder
// Create some code licenses. // Create some code licenses.
$licenses = License::factory(10)->create(); $licenses = License::factory(10)->create();
// Add 5 administrators. // Add administrators.
$administrator = UserRole::factory()->administrator()->create(); $administratorRole = UserRole::factory()->administrator()->create();
User::factory()->for($administrator, 'role')->create([ $testAccount = User::factory()->for($administratorRole, 'role')->create([
'email' => 'test@example.com', '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. // Add moderators.
$moderator = UserRole::factory()->moderator()->create(); $moderatorRole = UserRole::factory()->moderator()->create();
User::factory(5)->for($moderator, 'role')->create(); User::factory(5)->for($moderatorRole, 'role')->create();
// Add 100 users. // Add users
$users = collect(progress( progress(
label: 'adding users...', label: 'adding users...',
steps: $userCount, steps: $userCount,
callback: fn () => User::factory()->create() callback: fn () => User::factory()->create()
)); );
// Add 300 mods, assigning them to the users we just created. // get all users
$allUsers = $users->merge([$administrator, $moderator]); $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( $mods = collect(progress(
label: 'adding mods...', 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( $modVersions = collect(progress(
label: 'adding mods versions ...', label: 'adding mods versions ...',
steps: $modVersionCount, steps: $modVersionCount,