-1

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

CoderTn
  • 985
  • 2
  • 22
  • 49

3 Answers3

4

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
    }
}
matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
1

Add the following line in the beginning of seeder file

namespace Database\Seeders;

and then execute following in terminal

composer dumpautoload
SyedZaeem
  • 21
  • 1
  • 1
  • 4
0

Add

namespace Database\Seeders;

To both files.

It is standard PSR-4.

SVFCode
  • 19
  • 6
  • i just added namespace Database\Seeders; to both files here is why i got when i seed the database : Target class [Database\Seeders\MockNotificationSeeder] does not exist. – CoderTn Jun 15 '21 at 06:44
  • Rename MockNotification file to MockNotificationSeeder. In docs https://laravel.com/docs/8.x/seeding use this agreement. – SVFCode Jun 15 '21 at 07:03
  • already added the namespace Database/Seeders in both files and still getting the same error ! – CoderTn Jun 15 '21 at 07:04