#Having trouble with Laravel Seeders using factories for User model

7 messages · Page 1 of 1 (latest)

bold widget
#

Hello everyone, I'm currently facing some issues while trying to seed my User model using factories. Despite having the 'password' column defined in my UserFactory, I'm still receiving a "SQLSTATE[HY000]: General error: 1364 Field 'password' doesn't have a default value" error when I run the db:seed command.

Here's my UserFactory definition:

protected $model = User::class;

public function definition()
{
    return [
        'name' => $this->faker->name,
        'email' => $this->faker->unique()->safeEmail,
        'password' => Hash::make('password88'),
        // ...
    ];
}

protected function initialize()
{
    parent::initialize();
    $this->faker->locale('en_AU');
}

And my UserSeeder class:

public function run()
{
    User::factory(50)->create();
}

I've also set the 'name' and 'email' columns to nullable in the migration file, but the issue persists. Any help would be greatly appreciated!

Thank you.

steel stream
#

What does your migration look like?

bold widget
wooden oasis
#

have you added $fillable property on your model?

bold widget
# wooden oasis have you added `$fillable` property on your model?

fillable as follows in User model:

protected $fillable = [
'name',
'email',
'password',
'company_name',
'mobile',
'area',
'address',
'website_url',
'company_permalink',
'profile_image',
'company_logo',
'hero_image',
'contacts_id',
'role'
];

error still coming back:

 Illuminate\Database\QueryException 

  SQLSTATE[HY000]: General error: 1364 Field 'password' doesn't have a default value (SQL: insert into `users` (`magic`, `updated_at`, `created_at`) values (omXeA1yjncqKitJFHyGVNlBElNrxfqsi, 2023-04-11 14:23:59, 2023-04-11 14:23:59))

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:760
    756▕         // If an exception occurs when attempting to run a query, we'll format the error
    757▕         // message to include the bindings with SQL, which will make this exception a
    758▕         // lot more helpful to the developer instead of just the database's errors.
    759▕         catch (Exception $e) {
  ➜ 760▕             throw new QueryException(
    761▕                 $query, $this->prepareBindings($bindings), $e
    762▕             );
    763▕         }
    764▕     }

      +16 vendor frames 
  17  database/seeders/UserSeeder.php:17
      Illuminate\Database\Eloquent\Factories\Factory::create()

      +8 vendor frames 
  26  database/seeders/DatabaseSeeder.php:18
      Illuminate\Database\Seeder::call()
wooden oasis
#

I guess Laravel is called the unintentional factory class, where you placed your UserFactory?

bold widget
#

Found source of issue:

/*public function __construct(array $attributes = [])
    {
        // Generate a random string
        //$randomString = $this->generateRandomString();

        // Set the 'magic' column to the random string
        //$this->magic = $randomString;

        //parent::__construct($attributes);
    } */

had construct function in user setting magic string, commented it out and it passed the tests.