添加填充

main
liutk 2024-05-26 13:02:44 +08:00
parent abd0ae0029
commit 5a80f9cd06
4 changed files with 60 additions and 15 deletions

View File

@ -43,4 +43,8 @@ class User extends Authenticatable
{
return $this->hasMany(UserGift::class, 'user_id');
}
public function activityLogs(){
return $this->hasMany(UserActivity::class, 'user_id');
}
}

View File

@ -0,0 +1,34 @@
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\User>
*/
class UserActivityFactory extends Factory
{
/**
* The current password being used by the factory.
*/
protected static ?string $password;
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'activity_id' => 1,
'mark' => rand(50, 500),
'join_times' => rand(5, 10),
'right_times' => rand(0, 5),
'last_join_at' => now(),
];
}
}

View File

@ -24,21 +24,9 @@ class UserFactory extends Factory
public function definition(): array
{
return [
'name' => fake()->name(),
'email' => fake()->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => static::$password ??= Hash::make('password'),
'remember_token' => Str::random(10),
'nick_name' => fake()->name(),
'mini_openid' => Str::random(16),
'avatar' => env('APP_URL').'/default-avatar.png',
];
}
/**
* Indicate that the model's email address should be unverified.
*/
public function unverified(): static
{
return $this->state(fn (array $attributes) => [
'email_verified_at' => null,
]);
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace Database\Seeders;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use App\Models\User;
use App\Models\UserActivity;
class UserFactorySeeder extends Seeder
{
/**
* Seed the application's database.
*/
public function run(): void
{
User::factory()->has(UserActivity::factory()->count(1), 'activityLogs')->count(40)->create();
}
}