Laravel:“php artisan db:seed”不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29727510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:23:12  来源:igfitidea点击:

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

phplaravellaravel-5laravel-seeding

提问by CodeMan

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

我试图在数据库中运行“ServiceTableSeeder”表我收到一个错误消息。

I try run "php artisan db:seed"

我试着跑“ php artisan db:seed

Msg:

留言:

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

DatabaseSeeder .php

数据库浏览器 .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

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.

如何解决这个问题。我是 laravel 的新手,请指导我。

采纳答案by Arthur Samarcos

Considering that Service is a model you created, and that this model is inside the app folder, within the App namespace, try this:

考虑到 Service 是您创建的模型,并且该模型位于 app 文件夹内的 App 命名空间内,请尝试以下操作:

Fix your ServiceTableSeeder.php header:

修复您的 ServiceTableSeeder.php 标头:

<?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:

当您将模型移动到 app\models 时,您必须在每个模型文件中声明:

Models.php:

模型.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:

您是否使用 Composer 自动加载文件?如果是这样,请更新您的 composer.json 文件以包含您的模型位置:

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

And finally, run this in your command line:

最后,在命令行中运行它:

composer dump-autoload

回答by ankit patel

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

对于那些面临同样问题的人,请从 .env 文件中确认您的 APP_ENV 变量,因为如果我们设置,Laravel 不会让我们运行 db:seed

'APP_ENV = Production'

'APP_ENV = 生产'

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

因此,请确保将 APP_ENV 的值设置为“staging”或“local”,然后运行 ​​php artisan db:seed

回答by Marcin Nabia?ek

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

我认为问题在于您的ServiceTableSeeder.php文件。你应该确保这个文件中的类文件名是ServiceTableSeeder而不是DatabaseSeeder