Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

TzuSun67's avatar

Seeder User authentication

Hi All,

Sorry to bother you all constantly at the moment. I've just moved to AWS and finally managed to set it up however I can't seem to connect to the database through the ssh at the moment for some bizarre reason so figured I could create my admin user through a factory and seeder. The database does get seeded however when I come to login it says that the credentials are invalid.

User Factory

<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */
use App\User;
use Faker\Generator as Faker;
use Illuminate\Support\Str;

$factory->define(User::class, function (Faker $faker) {
    return [
        'name' => 'admin',
        'email' => '[email protected]',
        'email_verified_at' => now(),
        'password' => 'yIXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
        'bio' => $faker->userName,
        'experience' => $faker->userName,
        'years_played' => $faker->numberBetween(0,10),
        'gear' => $faker->userName,
        'userlevel' => 'admin',
        'avatar' => 'uploads/avatars/default.jpg',
        'remember_token' => Str::random(10),
    ];
});

User Seeder

<?php

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

class UserSeeder extends Seeder
{
    public function run()
    {
        Factory(User::class, 1) -> create();
    }
}

Main Seeder

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    public function run()
    {
         $this->call(UserSeeder::class);
    }
}

User Migration

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUsersTable extends Migration
{
    
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->string('bio');
            $table->string('experience')->default('beginner');
            $table->integer('years_played')->default('0');
            $table->string('gear');
            $table->string('userlevel');
            $table->string('avatar')->default('uploads/avatars/default.jpg');
            $table->rememberToken();
            $table->timestamps();
        });
    }
    
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

Any help on this would be brilliant!

Thanks for looking, Ben

0 likes
3 replies
MichalOravec's avatar

Something is just with you password, I am not sure right, but maybe it depends on key in .env file.

TzuSun67's avatar

Hi Michal,

What Key are you talking about exactly? The APP_KEY? It is already set to the base64 default.

TzuSun67's avatar

Hi have managed to get the user part to work through the seeder instead of the factory and it allows me to login as the admin user.

I have began seeding my website however I am now getting this error

   Illuminate\Database\QueryException  : SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`adv_ass
ign_2`.`comments`, CONSTRAINT `comments_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) (SQL: insert into `comments` (`user_id`, `product_id`, `comme
nts`, `likes`, `updated_at`, `created_at`) values (3, 3, Exercitationem quia tempora provident maxime quod autem., 16, 2020-04-25 18:43:40, 2020-04-25 18:43:40))

I understand the error however I am unsure of how to go about fixing. Is there a way to take users which are already seeded and add them onto a comment? I will then get this issue when it looks for product_id as each comment is assigned to a product.

here is my user factory

<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */
use App\User;
use Faker\Generator as Faker;
use Illuminate\Support\Str;

$factory->define(User::class, function (Faker $faker) {
    return [

    ];
});

User Seeder

<?php

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

class UserSeeder extends Seeder
{
    public function run()
    {
        Factory(User::class, 1) -> create();
    }
}

Product Factory

<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */

use Faker\Generator as Faker;
use App\Product;

$factory->define(Product::class, function (Faker $faker) {
    return [
        'make' => 'Marshall'||'fender'||'Vox',
        'Model' => 'DS30'||'BandMaster'||'AC30',
        'watt' => '30'||'60'||'100',
        'genre' => 'rock',
        'description' => $faker -> sentence(12,true),
        'dor' => $faker -> numberBetween(1950,2020),
        'brief' => $faker -> sentence(4,true),
        'retired' => $faker -> numberBetween(1950,2020),
        'star_rating' => $faker -> numberBetween(1,5),
        'image_1' => 'uploads/products/15858481040.jpg'||'uploads/products/15858481041.jpg',
        'image_2' => 'uploads/products/15858481042.jpg'||'uploads/products/15860111690.jpg',
        'image_3' => 'uploads/products/15860111691.jpg'||'uploads/products/15860111692.jpg',
    ];
});

Comment Factory

<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */
use App\Comment;
use Faker\Generator as Faker;
use Illuminate\Support\Str;

$factory->define(Comment::class, function (Faker $faker) {
    return [
        'user_id' => $faker -> numberBetween(1,20),
        'product_id' => $faker -> numberBetween(1,3),
        'comments' => $faker -> sentence(10,true),
        'likes' => $faker -> numberBetween(0,20),
    ];
});

DatabaseSeeder

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    public function run()
    {
         DB::table('users')->insert([
             'name' => 'admin',
        'email' => '[email protected]',
        'email_verified_at' => now(),
        'password' => bcrypt('jimtest'), // password
        'bio' => 'test',
        'experience' => 'test',
        'years_played' => '2',
        'gear' => 'test',
        'userlevel' => 'admin',
        'avatar' => 'uploads/avatars/default.jpg',
         ]);

        $this->call(ProductSeeder::class);
        $this->call(CommentSeeder::class);
    }
}

Any ideas on this?

Please or to participate in this conversation.