Laravel 5.3播种机 - 未定义的方法表?

时间:2016-09-28 16:58:29

标签: php mysql laravel laravel-5 laravel-seeding

我正在尝试学习Laravel,但似乎文档是用错误的示例编写的...我想创建一个表迁移,运行它,并用一些内容播种它。

首先:

php artisan make:migration create_projects_and_tasks_tables

具有以下内容:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateProjectsAndTasksTables extends Migration
{
    /**
      * Run the migrations.
      *
      * @return void
      */
      public function up()
      {
          Schema::create('projects', function (Blueprint $table) {
              $table->increments('id');
              $table->string('name')->default('');
              $table->string('slug')->default('');
              $table->timestamps();
      });

      Schema::create('tasks', function (Blueprint $table) {
          $table->increments('id');
          $table->integer('project_id')->unsigned()->default(0);
          $table->foreign('project_id')->references('id')->on('projects')->onDelete('cascade');
          $table->string('name')->default('');
          $table->string('slug')->default('');
          $table->boolean('completed')->default(false);
          $table->text('description');
          $table->timestamps();
      });
  }

  /**
    * Reverse the migrations.
    *
    * @return void
    */
    public function down()
    {
        Schema::drop('tasks');
        Schema::drop('projects');
     }
}

迁移好了。所以我想种子项目表。

首先:

php artisan make:seeder ProjectsTableSeeder

内容:

<?php

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

class ProjectsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $projects = array(
            ['id' => 1, 'name' => 'Project 1', 'slug' => 'project-1', 'created_at' => new DateTime, 'updated_at' => new DateTime],
            ['id' => 2, 'name' => 'Project 2', 'slug' => 'project-2', 'created_at' => new DateTime, 'updated_at' => new DateTime],
            ['id' => 3, 'name' => 'Project 3', 'slug' => 'project-3', 'created_at' => new DateTime, 'updated_at' => new DateTime]
        );
        DB::table('projects')->insert($projects);
    }
}

所有设置,我尝试回滚迁移,迁移并播种它:

php artisan migrate:refresh --seed
Rolled back: 2016_09_28_160459_create_projects_and_tasks_tables
Rolled back: 2014_10_12_100000_create_password_resets_table
Rolled back: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_100000_create_password_resets_table
Migrated: 2016_09_28_160459_create_projects_and_tasks_tables

[Symfony\Component\Debug\Exception\FatalThrowableError]                   
  Call to undefined method Illuminate\Database\MySqlConnection::setTable()

就是这样。我的表是空的,方法DB :: table()似乎在框架中不再存在,即使5.3文档显示它。我该怎么办?

我正在使用Laravel Homestead流浪盒,因此php版本或作曲家不是问题。我也使用MySQL作为我的数据库驱动程序。

4 个答案:

答案 0 :(得分:1)

编写播种器类后,可以使用db:seed Artisan命令为数据库设定种子。默认情况下,db:seed命令运行DatabaseSeeder类,该类可用于调用其他种子类。但是,您可以使用--class选项指定要单独运行的特定播种器类:

php artisan db:seed

php artisan db:seed --class=UsersTableSeeder

此外,您可以为DatabaseSeeder.php文件的调用方法提供播种器类引用

php artisan db:seed

将运行DatabaseSeeder.php文件并调用run()meothd

所以在这里你可以提供种子来源列表,如关注

public function run()
{
     $this->call(CountriesTableSeeder::class);
     $this->call(VendorTypesTableSeeder::class);
}

参考:seeding

答案 1 :(得分:0)

默认情况下,传递给--seed命令时的migrate:refresh选项将运行

{YOUR_APP}/database/seeds/DatabaseSeeder.php

make:migration artisan命令将创建您的种子,但它不会将播种器添加到DatabaseSeeder.php文件中。您需要手动执行此操作。

但是,如果您要使用migrate:refresh指定特定的播种机,请使用--seeder=YourSeeder选项。

答案 2 :(得分:0)

将以下内容添加到播种机类的顶部:

use Illuminate\Support\Facades\DB;

不知何故,DB不在范围内。

答案 3 :(得分:0)

我得到了完全相同的错误。这显然是由错字引起的。尝试将所有数据种子表与其他表进行比较。您可能放了\ DB :: class而不是\ DB :: table。

相关问题