Randy_Johnson's avatar

Help with Unifid database Seeder! Eloquent

I started with this but too many users are created

        \App\Models\User::factory()->create([
            'name' => 'Admin',
            'email' => '[email protected]',
            'password' => bcrypt('secret'),
        ]);

        \App\Models\User::factory()->create([
            'name' => 'Volunteer',
            'email' => '[email protected]',
            'password' => bcrypt('secret'),
        ]);

        User::factory()->count(48)->create();
        Detail::factory()->count(40)->create();
        Host::factory()->count(30)->create();
        Contact::factory()->count(40)->create();
        Message::factory()->count(40)->create();

So using GPT I got to this, but still too many users are created, should I be concerned or should I just continue since the data is there anyway so it doesn't really matter. (Really need to learn how to handle dummy data like a PRO)

        \App\Models\User::factory()->create([
            'name' => 'Admin',
            'email' => '[email protected]',
            'password' => bcrypt('secret'),
        ]);

        \App\Models\User::factory()->create([
            'name' => 'Volunteer',
            'email' => '[email protected]',
            'password' => bcrypt('secret'),
        ]);

        User::factory()->count(48)->create();

        $users = User::all();

        Detail::factory()->count(40)->make()->each(function ($host) use ($users) {
            $host->user_id = $users->random()->id;
            $host->save();
        });

        Host::factory()->count(10)->make()->each(function ($host) use ($users) {
            $host->user_id = $users->random()->id;
            $host->save();
        });

        Contact::factory()->count(20)->make()->each(function ($contact) use ($users) {
            $contact->sender_id = $users->random()->id;
            $contact->receiver_id = $users->random()->id;
            $contact->save();
        });

        $contacts = Contact::all();

        // Create messages for each contact
        Message::factory()->count(40)->make()->each(function ($message) use ($contacts) {
            $message->contact_id = $contacts->random()->id;
            $message->save();
        });
0 likes
7 replies
Snapey's avatar

so which of your factory is also creating users?

Randy_Johnson's avatar

So the problem here I feel is that \App\Models\User::factory() is called a user is being created to create relationship. Would it be better to just have a range of 1, 100? Instead of the method I am currently using.

class UserFactory 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 [
            'name' => fake()->name(),
            'email' => fake()->unique()->safeEmail(),
            'email_verified_at' => now(),
            'password' => static::$password ??= Hash::make('password'),
            'remember_token' => Str::random(10),
        ];
    }
class MessageFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        return [
            'contact_id' => \App\Models\Contact::factory(),  // Assuming you have a Contact model
            'body' => $this->faker->paragraph,   // Generates a random paragraph for the body
            'created_at' => now(),
            'updated_at' => now(),
        ];
    }
class HostFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        return [
            'user_id' => \App\Models\User::factory(),  // Assuming you have a User model
            'type' => $this->faker->randomElement(['workaway', 'coachsurfing', 'commune']), // Example types
            'name' => $this->faker->company,  // Generates a random company name
            'description' => $this->faker->sentence,  // Generates a random sentence for description
            'country' => $this->faker->country,  // Generates a random country
            'region' => $this->faker->state,  // Generates a random state/region
            'city' => $this->faker->city,  // Generates a random city
            'created_at' => now(),
            'updated_at' => now(),
        ];
class DetailFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        return [
            'user_id' => \App\Models\User::factory(),  // Assuming you have a User model
            'forename' => $this->faker->firstName,  // Generates a random first name
            'surname' => $this->faker->lastName,  // Generates a random last name
            'telephone' => $this->faker->phoneNumber,  // Generates a random phone number
            'created_at' => now(),
            'updated_at' => now(),
        ];
    }
class ContactFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        return [
            'sender_id' => \App\Models\User::factory(),  // Assuming you have a User model
            'receiver_id' => \App\Models\User::factory(),  // Assuming you have a User model
            'head' => $this->faker->sentence,
            'created_at' => now(),
            'updated_at' => now(),
        ];
    }
Snapey's avatar

Your HostFactory and DetailFactory both create users

Randy_Johnson's avatar

@Snapey What would be the correct way to handle this, to just say, create 100 users and then on the detail and and host to randomly pick a number between 1 - 100?

                                                                                               
  ┌───────────────────┐    ┌──────────────────┐    ┌──────────────────┐   ┌─────────────────┐  
  │                   │    │                  │    │                  │   │                 │  
  │  details          │    │                  │    │                  │   │  messages       │  
  │                   │    │  users           │    │   contacts       │   │                 │  
  │                   │    │                  │    │                  ├───►                 │  
  │                   ◄────┤                  ├────►                  │   │                 │  
  │                   │    │                  │    │                  │   │                 │  
  │                   │    │                  │    │                  │   │                 │  
  │                   │    │                  │    │                  │   │                 │  
  │                   │    │                  │    │                  │   │                 │  
  │                   │    │                  │    │                  │   │                 │  
  │                   │    │                  │    │                  │   │                 │  
  └───────────────────┘    └──────────┬───────┘    └──────────────────┘   └─────────────────┘  
                                      │                                                        
                                      │                                                        
                                      │                                                        
                            ┌─────────▼───────┐                                                
                            │                 │                                                
                            │                 │                                                
                            │  hosts          │                                                
                            │                 │                                                
                            │                 │                                                
                            │                 │                                                
                            │                 │                                                
                            │                 │                                                
                            │                 │                                                
                            └─────────────────┘                                                
                                                                                               

Please or to participate in this conversation.