Gobinath M
Gobinath M

Reputation: 2021

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.

Upvotes: 4

Views: 9813

Answers (3)

Arthur Samarcos
Arthur Samarcos

Reputation: 3299

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

Upvotes: 2

ankit patel
ankit patel

Reputation: 1918

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

Upvotes: 2

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111829

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

Upvotes: 2

Related Questions