Reputation: 77
I'm trying to learn Laravel, but it seems the documentation is written with faulty examples... I want to create a table migration, run it, and seed it with some content.
First:
php artisan make:migration create_projects_and_tasks_tables
With the following content:
<?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');
}
}
It migrated Ok. So I want to seed the projects table.
First:
php artisan make:seeder ProjectsTableSeeder
The contents:
<?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);
}
}
All set, I tried to rollback the migration, migrate and seed it:
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()
And that's it. My table is empty, and the method DB::table() seems to not exist anymore in the framework, even if the 5.3 docs shows it. What can I do?
I'm using the Laravel Homestead vagrant box, so php version or composer isn't the issue. I'm also using MySQL as my database driver.
Upvotes: 0
Views: 1207
Reputation: 143
I got the exact same error. This was caused by typo obviously. Try to compare all the dataseed tables with other tables. You might have put \DB::class instead of \DB::table.
Upvotes: 0
Reputation: 305
Add the following to the top of your seeder class:
use Illuminate\Support\Facades\DB;
Somehow, DB is not within the scope.
Upvotes: 0
Reputation: 7615
Once you have written your seeder classes, you may use the db:seed Artisan command to seed your database. By default, the db:seed command runs the DatabaseSeeder class, which may be used to call other seed classes. However, you may use the --class option to specify a specific seeder class to run individually:
php artisan db:seed
php artisan db:seed --class=UsersTableSeeder
Also You Can provide a seeder class reference to call method of DatabaseSeeder.php file
php artisan db:seed
will run the DatabaseSeeder.php file and invoke the run() meothd
So here you can provide list of Seed source like follow
public function run()
{
$this->call(CountriesTableSeeder::class);
$this->call(VendorTypesTableSeeder::class);
}
Reference : seeding
Upvotes: 1
Reputation: 81
By default, the --seed
option when passed to the migrate:refresh
command will run
{YOUR_APP}/database/seeds/DatabaseSeeder.php
The make:migration
artisan command will create your seed, but it won't add the seeder to the DatabaseSeeder.php file. You'll need to manually do this.
However, if you want to specify a particular seeder with migrate:refresh
, then use the --seeder=YourSeeder
option.
Upvotes: 0