-1

I am trying to add a super user in my laravel project. I have but the error occors.

The use statement with non-compound name 'SuperAdminSeeder' has no effect", "E:\github\LARAVEL\Deal-Ocean\database\seeds\DatabaseSeeder.php", ["E:\github\LARAVEL\Deal-Ocean\vendor\composer/../../database/seeds/DatabaseSeeder.php"]

SuperAdminSeeder.php

<?php

use App\Role;
use App\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class SuperAdminSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $role = Role::create(['name' => 'super_admin']);
        $user = User::create(['email' => 'imrulhasan273@email.com', 'imrulhasan' => bcrypt('secret')]);
        DB::table('role_user')->insert(['user_id' => $user->id, 'role_id' => $role->id]);
    }
}

On the above query I am creating a super user with id name and role. And also create their role_id and user_id in pivot table.

DatabaseSeeder.php

<?php

use SuperAdminSeeder;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        // $this->call(UserSeeder::class);
        $this->call(SuperAdminSeeder::class);
    }
}

Chilarai
  • 1,842
  • 2
  • 15
  • 33
Backstreet Imrul
  • 102
  • 2
  • 15
  • Get rid of the `use SuperAdminSeeder;` line, run a `composer dumpautoload` just to make sure and then try again ;) – Paladin Jul 21 '20 at 05:21
  • I guess, `Migrations ` and `Seeders` do not use `PSR-4` autoloading. They `require` the files from the respective folders instead. So you can remove the `use SuperAdminSeeder` statement from the top. – ash__939 Jul 21 '20 at 05:27
  • Thanks. This problem is solved may be..But another exception comes " PDOException::("SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value")" This is my roles table ``` Schema::create('roles', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('display_name'); $table->timestamps(); });``` – Backstreet Imrul Jul 21 '20 at 05:32
  • `'imrulhasan' => bcrypt('secret')` Should that be `'password' => bcrypt('secret')`? Do you have a column named `imrulhasan` in your table? Also, you need to add a `name` for your user – brombeer Jul 21 '20 at 05:40
  • Thanks everyone... Problem solved.... I am grateful to all of you – Backstreet Imrul Jul 21 '20 at 05:41
  • Sorry bro... I have mistakenly put name instead of column name... Thanks for your help.. Problem solved now. I I had problems here. THe column name and the `use SuperAdminSeeder`. Now everything works fine – Backstreet Imrul Jul 21 '20 at 05:43

1 Answers1

1

Your DatabaseSeeder is not in a dedicated namespace; it is in the global namespace. Because of this, you don't need to use any other classes that are also in the global namespace.

Since SuperAdminSeeder is also in the global namespace, the use SuperAdminSeeder; statement in your DatabaseSeeder does not do anything (except cause the error).

Remove the use SuperAdminSeeder; statement.

patricus
  • 59,488
  • 15
  • 143
  • 145