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

I am trying to start the "ServiceTableSeeder" table in the database, I got an error.

I am trying to 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 problem. I am new to laravel, can someone please guide me.

+3


source to share


3 answers


Considering that Service is a model that you created and that model is inside the application folder in the applications namespace, try this:

Correct the 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.'
      )
    );

  }
}

      

When you have moved your models to app \ models, you must declare that in each model file:

Models.php:

 namespace App\Models;

      



And in the seed file use:

 use App\Models\Service.php;

      

Are you using composer to automatically download your files? If so, update your composer.json file to point to the location of your models:

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

      

Finally, run this on the command line:

composer dump-autoload

      

+1


source


For those facing the same problem, validate your APP_ENV variable from the .env file, because Laravel doesn't allow us to run db: seed if we have installed

'APP_ENV = Production'



for database records.

So, make sure you set APP_ENV to 'staging' or 'local' and then run php artisan db: seed

+2


source


I think the problem is with your file ServiceTableSeeder.php

. You have to make sure that the name of the class file is in that file ServiceTableSeeder

and notDatabaseSeeder

+1


source







All Articles