F
F
fapchat2022-04-21 23:09:06
PHP
fapchat, 2022-04-21 23:09:06

How to fix "Target class [UserSeeder] does not exist" error?

Here is the DatabaseSeeder.php code:

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

use UserSeeder;
use PostSeeder;

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

Here is UsersSeeder.php:
<?php
namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

class UserSeeder extends Seeder
{
    public function run()
    {
        // for ($i = 0; $i < 10; $i++) {
        DB::table('users')->insert([
            'first_name' => Str::random(10),
            'second_name' => Str::random(10),
            'email' => Str::random(10) . '@gmail.com',
            'password' => Hash::make(Str::random(12)),
        ]);
        // }
    }
}

Laravel is my 9th. Updated it is fine. Here is composer.json:
{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "require": {
        "php": "^8.0.2",
        "doctrine/dbal": "^3.3",
        "guzzlehttp/guzzle": "^7.2",
        "laravel/framework": "^9.2",
        "laravel/sanctum": "^2.14.1",
        "laravel/tinker": "^2.7"
    },
    "require-dev": {
        "fakerphp/faker": "^1.9.1",
        "laravel/sail": "^1.0.1",
        "mockery/mockery": "^1.4.4",
        "nunomaduro/collision": "^6.1",
        "phpunit/phpunit": "^9.5.10",
        "spatie/laravel-ignition": "^1.0"
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-update-cmd": [
            "@php artisan vendor:publish --tag=laravel-assets --ansi --force"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ]
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true
    },
    "minimum-stability": "dev",
    "prefer-stable": true
}

On the Internet there is advice about the team composer dump-autoload. So, she didn't help me.
If you write php artisan migrate:fresh --seed, then the error mentioned in the title of this question will occur!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lil_koi, 2022-04-22
@fapchat

Replace with + your file is called UsersSeeder.php , and the class name is UserSeeder
use UserSeeder;
use Database\Seeders\UsersSeeder;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question