2016-09-28 159 views
0

我正在嘗試學習Laravel,但它似乎是用錯誤的示例編寫的文檔...我想創建一個表遷移,運行它,併爲其添加一些內容。Laravel 5.3 Seeder - 未定義的方法表?

第一:

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家園vagrant框,所以PHP版本或作曲家是不是問題。我也使用MySQL作爲我的數據庫驅動程序。

+0

嘗試:php artisan make:seeder --class = ProjectsTableSeeder – Chintan7027

+0

也許'使用DB;'?你有沒有改變一個DatabaseSeeder.php文件? –

+0

在'\ DB'前放置一個反斜槓。 – Ohgodwhy

回答

1

一旦你編寫了你的​​播種器類,你可以使用db:seed Artisan命令爲你的數據庫播種。默認情況下,db:seed命令運行DatabaseSeeder類,該類可用於調用其他種子類。但是,您可以使用--class選項來指定一個特定的播種機類單獨運行:

php artisan db:seed 

php artisan db:seed --class=UsersTableSeeder 

,你也可以提供一個播種機類引用調用DatabaseSeeder.php的方法文件

PHP工匠DB:種子

將運行DatabaseSeeder.php文件並調用運行()meothd

所以在這裏,你可以提供種源的名單一樣遵循

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

參考:seeding

+1

這不應該是一個答案 –

0

默認情況下,當傳遞給migrate:refresh命令--seed選項將運行

{YOUR_APP}/database/seeds/DatabaseSeeder.php

make:migration工匠命令將創建你的後裔,但不會將播種器添加到DatabaseSeeder.php文件。您需要手動執行此操作。

但是,如果您想用migrate:refresh指定特定播種機,請使用--seeder=YourSeeder選項。

0

以下添加到您的播種機類的頂部:

use Illuminate\Support\Facades\DB; 

不知何故,DB不在範圍之內。

相關問題