mercredi 28 septembre 2016

Larval 5.3 Seeder - undefined method table?

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.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire