4

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.

user2094178
  • 9,204
  • 10
  • 41
  • 70
CodeMan
  • 1,941
  • 6
  • 26
  • 44

3 Answers3

2

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

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
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 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
Qazi
  • 5,015
  • 8
  • 42
  • 62
Arthur Samarcos
  • 3,262
  • 22
  • 25
  • i try your step but i got "[symfony\component|Debug\Exception\FetalErrorException] class 'Service' Not found . Pls guide me – CodeMan Apr 19 '15 at 15:03
  • @GobinathMahalingam I believe that you need to declare the namespace of the model. I included that in the answer. – Arthur Samarcos Apr 19 '15 at 15:11
  • @authur samarcos: I'm using laravel new version . I created models folder inside App folder. And created service model page. I try "use App\service;" and also "use App\models \Services" but I got same error msg – CodeMan Apr 21 '15 at 02:07
  • I think I make mistake to create models pls guide me – CodeMan Apr 21 '15 at 02:07
2

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

ankit patel
  • 1,888
  • 11
  • 12