【问题标题】:I cant seed data in laravel我不能在 laravel 中播种数据
【发布时间】:2017-08-03 04:52:15
【问题描述】:

H 有 2 个表,用户和作者,我有各自的模型。但是当我尝试播种时会出现以下错误:

类“作者”未在 /var/www/html/shopping/database/seeds/AuthorsTableSeeder.php 上线 12

[Symfony\Component\Debug\Exception\FatalErrorException] 类 找不到“作者”

之前我的 users 表也有同样的问题,但我通过添加 use Illuminate\Foundation\Auth\User; 解决了这个问题

这是我的作者 seeder.php

<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;


Class AuthorsTableSeeder extends Seeder {

    public function run()
    {
DB::table('authors')->delete();

        Author::create(array(
            'name' => 'Lauren',
            'surname'=>'Oliver'
        ));

        Author::create(array(
            'name' => 'Stephenie',
            'surname'=>'Meyer'
        ));

        Author::create(array(
            'name' => 'Dan',
            'surname'=>'Brown'
        ));

    }

}

我的databaseseeder.php在下面

<?php

use Illuminate\Database\Seeder;


class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
    Eloquent::unguard();

    $this->call('UsersTableSeeder');
    $this->command->info('Users table seeded!');
    $this->call('AuthorsTableSeeder');
    $this->command->info('Authors table seeded!');

    }
}

这也是我的website folder

【问题讨论】:

  • 也许给我们看一张你的文件目录的截图。这将有助于诊断您的问题,而不是让我们猜测文件名可能是什么。
  • 我们可以看到您的 Author.php 文件中的代码吗?
  • 是的,你的 Author.php 文件中的类是什么?
  • 好的..我会上传的
  • 得到了答案..我已将其发布在答案部分

标签: php laravel seeding


【解决方案1】:

我找到了答案。问题出在语法上。我使用了以下,它的工作原理

<?php namespace App;
use Illuminate\Database\Eloquent\Model;

Class Author extends Model {

protected $fillable = array('name','surname');
protected $table = 'authors';



}

我替换了以下代码的位置 protected $fillable = array('name','surname'); 受保护的 $table = '作者';

我使用了“模型”而不是“雄辩”,它奏效了

【讨论】:

    【解决方案2】:

    AuthorsTableSeeder处添加型号名称Author

    use App\Author;
    

    因为,您使用的是作者模型,而没有在顶部添加。

    【讨论】:

    • 我用过它.. 但它显示的 Class 'App\Author' not found
    • 作者是我的模型.. 我没有模型文件夹。我的模型在 app 文件夹下
    • 是否命名为 Authors
    • 模型名称 author.php
    • sagar gautam.. 是否有必要使用该删除,以便我们更新表后不会添加重复值
    【解决方案3】:

    你应该试试这个:

    <?php
    use Illuminate\Database\Seeder;
    use Illuminate\Database\Eloquent\Model;
    
    
    Class AuthorsTableSeeder extends Seeder {
    
        public function run()
        {
            DB::table('authors')->delete();
    
            DB::table('authors')->insert(array(
                'name' => 'Lauren',
                'surname'=>'Oliver'
            ));
    
            DB::table('authors')->insert(array(
                'name' => 'Stephenie',
                'surname'=>'Meyer'
            ));
    
            DB::table('authors')->insert(array(
                'name' => 'Dan',
                'surname'=>'Brown'
            ));
    
        }
    
    }
    

    希望这对你有用!!!

    【讨论】:

    猜你喜欢
    • 2019-09-12
    • 2015-09-25
    • 2014-05-04
    • 2016-01-20
    • 2014-03-01
    • 1970-01-01
    • 2019-01-19
    • 2016-02-10
    相关资源
    最近更新 更多