我正在嘗試學習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作爲我的數據庫驅動程序。
嘗試:php artisan make:seeder --class = ProjectsTableSeeder – Chintan7027
也許'使用DB;'?你有沒有改變一個DatabaseSeeder.php文件? –
在'\ DB'前放置一個反斜槓。 – Ohgodwhy