0

I am trying to follow this documentation on how to make a seeder work

Essentially just trying to get the demo thing they have working.

When I run php artisan db:seed I get this error enter image description here

enter image description here

KanbanSeeder.php

<?php

namespace Database\Seeders;

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

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeders.
     *
     * @return void
     */
    public function run()
    {
        DB::table('users')->insert([
            'name' => Str::random(10),
            'email' => Str::random(10).'@gmail.com',
            'password' => Hash::make('password'),
        ]);
    }
}

DatabaseSeeder.php

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        // \App\Models\User::factory(10)->create();
        $this->call(KanbanSeeder::class);
    }
}

Any insight or links to a tutorial that might be more helpful for a novice than laravel documentation is much appreciated.

Jakub
  • 1,260
  • 1
  • 13
  • 40
  • 2
    why did you name it `DatabaseSeeder` when the filename is `KanbanSeeder.php`? there is already a class in that namespace named `DatabaseSeeder` – lagbox Feb 06 '21 at 21:25
  • I did not create the DatabaseSeeder.php file, it was just there by default with the laravel installation. I couldn't get things to work the first time running the seed command. So I found a youtube tutorial and the guy wrote `$this->call(KanbanSeeder::class);` inside the database seeder and said this is the default one that executes? It worked on his screen. https://youtu.be/yMOOmvUm-gE – Jakub Feb 06 '21 at 21:32
  • I am more than happy to start from scratch. All I am trying to do is insert something into db with the seeder stuff haha just to see how it works. I am really new to laravel, so I don't really intuitively understand the error messages – Jakub Feb 06 '21 at 21:33
  • fix class seeder name first , class name and file name are different – Mohamed Ahmed Feb 06 '21 at 21:37
  • 1
    I didn't say you created the `DatabaseSeeder.php` file ... why did you name the class `DatabaseSeeder` in the file named `KanbanSeeder.php`? the class name has to match that otherwise the autoload won't find it ... and you already have a class named `DatabaseSeeder` in that namespace so you can't name it that ... the class would be named `KanbanSeeder` – lagbox Feb 06 '21 at 21:52
  • 1
    Aah I see what you mean, okay I fixed it now. I didin't even notice that >.< now it runs the seed successfully, but it doens't insert the data. – Jakub Feb 07 '21 at 01:20
  • What do you mean by class name and file name are different? – Jakub Feb 07 '21 at 01:21
  • 1
    the file named `KanbanSeeder.php` should have the class named `KanbanSeeder` in it ... this is how PSR4 autoloading works – lagbox Feb 07 '21 at 04:08
  • Everything works now haha , thanks so much for the help! – Jakub Feb 07 '21 at 05:55

1 Answers1

0

The answer is what @lagbox wrote in comments.

I had to change class DatabaseSeeder extends Seeder

to class KanbanSeeder extends Seeder

Jakub
  • 1,260
  • 1
  • 13
  • 40