无法使用Laravel 4迁移表

时间:2015-09-16 23:36:31

标签: laravel laravel-4 migration migrate

当我尝试迁移我创建的表时,我收到此错误

PHP Fatal error:  Class 'Users' not found in /var/www/html/laravel/vendor/laravel/
framework/src/Illuminate/Database/Migrations/Migrator.php on line 301
{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException",
"message":"Class 'Users' not found","file":"\/var\/www\/html\/laravel\/vendor
\/laravel\/framework\/src\/Illuminate\/Database\/Migrations\/Migrator.php",
"line":301}}

这是我的代码:

用户表:

<?php

//Users Table
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateUsersTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function(Blueprint $table) {
            $table->increments('id');
            $table->string('username');
            $table->string('email');
            $table->string('password');
            $table->timestamps();
        });
    }


    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down(){
    }

}

帖子表:

<?php

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

class CreatePostsTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function(Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->text('body');
            $table->string('m_keyword');
            $table->string('m_disc');
            $table->string('slug');
            $table->integer('user_id');
            $table->timestamps();
        });
    }


    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
    }

}

http://laravel.io/bin/zj31n

2 个答案:

答案 0 :(得分:0)

如果在运行迁移时遇到上述错误,请运行以下命令

composer dump-autoload 

欲了解更多信息, http://laravel.com/docs/master/migrations#running-migrations

答案 1 :(得分:0)

可能出现问题的是你没有打电话

php artisan migrate:rollback

php artisan migrate

尝试强制删除表格并尝试再次迁移。

另一件事。 &#39; user_id &#39; &#39; 帖子中的列&#39; table应该像这样连接到users表:

Schema::table('posts', function(Blueprint $table) {
   $table->foreign('user_id')->references('id')->on('users');
});

我会像这样调用create table列:

$table->unsignedInteger('user_id');

$table->integer('user_id')->unsigned();