skip to Main Content

I am try to run “ServiceTableSeeder” table in database i got an error msg.

I try run “php artisan db:seed

Msg:

[symfonycomponent|DebugExceptionFetalErrorException]
cannot redeclare DatabaseSeeder::run()

DatabaseSeeder .php

<?php

use IlluminateDatabaseSeeder;
use IlluminateDatabaseEloquentModel;

class DatabaseSeeder extends Seeder {

    /**
     * Run the database seeds.
     *
     * @return void
     */
     public function run()
     {
         Eloquent::unguard();
         $this->call('ServiceTableSeeder');

     }

}

ServiceTableSeeder.php

<?php

class ServiceTableSeeder extends Seeder {

  public function run()
  {
    Service::create(
      array(
        'title' => 'Web development',
        'description' => 'PHP, MySQL, Javascript and more.'
      )
    );

    Service::create(
      array(
        'title' => 'SEO',
        'description' => 'Get on first page of search engines with our help.'
      )
    );

  }
}

how to fix this issue .i am new in laravel anyone please guide me.

3

Answers


  1. I think the problem is your ServiceTableSeeder.php file. You should make sure the class filename in this file is ServiceTableSeeder and not DatabaseSeeder

    Login or Signup to reply.
  2. Considering that Service is a model you created, and that this model is inside the app folder, within the App namespace, try this:

    Fix your ServiceTableSeeder.php header:

    <?php
    use IlluminateDatabaseSeeder;
    use AppService;
    
    class ServiceTableSeeder extends Seeder {
    
      public function run()
      {
        Service::create(
          array(
            'title' => 'Web development',
            'description' => 'PHP, MySQL, Javascript and more.'
          )
        );
    
        Service::create(
          array(
            'title' => 'SEO',
            'description' => 'Get on first page of search engines with our help.'
          )
        );
    
      }
    }
    

    As you have moved your models to appmodels, you must declare that in each model file:

    Models.php:

     namespace AppModels;
    

    And in your seed file, use:

     use AppModelsService.php;
    

    Are you using composer to autoload your files? If so, update your composer.json file to include your models location:

    "autoload": {
        "classmap": [
            "database",
            "app/Models"
        ],
        "psr-4": {
            "App\": "app/"
        }
    },
    

    And finally, run this in your command line:

    composer dump-autoload
    
    Login or Signup to reply.
  3. For those who are facing the same issue, confirm your APP_ENV variable from .env file cause Laravel don’t let us to run db:seed if we set

    ‘APP_ENV = Production’

    for the sake of database records.

    So, make sure you set value of APP_ENV to ‘staging’ or ‘local’ and then run php artisan db:seed

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search