Skip to content

Commit

Permalink
填充用户和话题数据
Browse files Browse the repository at this point in the history
  • Loading branch information
summerblue committed Mar 6, 2022
1 parent 6c8f8a3 commit 9a30536
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 24 deletions.
8 changes: 7 additions & 1 deletion database/factories/TopicFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ class TopicFactory extends Factory

public function definition()
{
$sentence = $this->faker->sentence();

return [
// $this->faker->name,
'title' => $sentence,
'body' => $this->faker->text(),
'excerpt' => $sentence,
'user_id' => $this->faker->randomElement([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
'category_id' => $this->faker->randomElement([1, 2, 3, 4]),
];
}
}
29 changes: 14 additions & 15 deletions database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,31 @@
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\User>
*/
class UserFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
// 用户的默认头像
$avatars = [
'https://cdn.learnku.com/uploads/images/201710/14/1/s5ehp11z6s.png',
'https://cdn.learnku.com/uploads/images/201710/14/1/Lhd1SHqu86.png',
'https://cdn.learnku.com/uploads/images/201710/14/1/LOnMrqbHJn.png',
'https://cdn.learnku.com/uploads/images/201710/14/1/xAuDMxteQy.png',
'https://cdn.learnku.com/uploads/images/201710/14/1/ZqM7iaP4CR.png',
'https://cdn.learnku.com/uploads/images/201710/14/1/NDnzMutoxX.png',
];

return [
'name' => $this->faker->name(),
'email' => $this->faker->unique()->safeEmail(),
'name' => $this->faker->name,
'email' => $this->faker->unique()->safeEmail,
'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'remember_token' => Str::random(10),
'introduction' => $this->faker->sentence(),
'avatar' => $this->faker->randomElement($avatars),
];
}

/**
* Indicate that the model's email address should be unverified.
*
* @return static
*/
public function unverified()
{
return $this->state(function (array $attributes) {
Expand Down
8 changes: 2 additions & 6 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@

class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
// \App\Models\User::factory(10)->create();
$this->call(UsersTableSeeder::class);
$this->call(TopicsTableSeeder::class);
}
}
3 changes: 1 addition & 2 deletions database/seeders/TopicsTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class TopicsTableSeeder extends Seeder
{
public function run()
{
Topic::factory()->count(10)->create();
Topic::factory()->count(100)->create();
}
}

22 changes: 22 additions & 0 deletions database/seeders/UsersTableSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\User;

class UsersTableSeeder extends Seeder
{
public function run()
{
// 生成数据集合
User::factory()->count(10)->create();

// 单独处理第一个用户的数据
$user = User::find(1);
$user->name = 'Summer';
$user->email = '[email protected]';
$user->avatar = 'https://cdn.learnku.com/uploads/images/201710/14/1/ZqM7iaP4CR.png';
$user->save();
}
}

0 comments on commit 9a30536

Please sign in to comment.