Search code examples
phplaravellaravel-seeding

Target class [Database\Seeders\MockNotification] does not exist in Laravel


i am using a seeder class to seed a table in the database

<?php
namespace Database\Seeders;
class MockNotification extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //
        Notification::factory()->times(2)->create();
    }
}

i am calling this class in the DatabaseSeeder


<?php
namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Database\Seeders\NotificationTypeSeeder;
use Database\Seeders\MockNotification;
class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        // $this->call(UserSeeder::class);
        $this->call([NotificationTypeSeeder::class]);
        $this->call([MockNotification::class]);
    }
}

i am getting this error

Target class [Database\Seeders\MockNotification] does not exist.

while i already imported MockNotification classs in the DatabaseSeeder file


Solution

  • Your problem is really simple to solve, you have to have your MockNotification class in LARAVEL_ROOT_FOLDER/database/seeders and then add this to the top of your class namespace Database\Seeders;.

    Your DatabaseSeeder class should also have namespace Database\Seeders;. It is needed for composer to do PSR-4 autoloading.

    Your seeder should be like:

    namespace Database\Seeders;
    
    use Illuminate\Database\Seeder;
    
    class MockNotification extends Seeder
    {
        /**
         * Run the database seeds.
         *
         * @return void
         */
        public function run()
        {
            Notification::factory()->times(2)->create(); // Add this use on top
        }
    }