Search code examples
phplaravellaravel-5laravel-seeding

Laravel :"php artisan db:seed" doesn't work


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

I try run "php artisan db:seed"

Msg:

[symfony\component|Debug\Exception\FetalErrorException]
cannot redeclare DatabaseSeeder::run()

DatabaseSeeder .php

<?php

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

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.


Solution

  • 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 Illuminate\Database\Seeder;
    use App\Service;
    
    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 app\models, you must declare that in each model file:

    Models.php:

     namespace App\Models;
    

    And in your seed file, use:

     use App\Models\Service.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