Php 无法在laravel中创建表
我有一个问题,我不知道迁移和种子数据库是否在laravel(php artisan)中工作,我有一个模型:Php 无法在laravel中创建表,php,mysql,laravel,laravel-4,artisan-migrate,Php,Mysql,Laravel,Laravel 4,Artisan Migrate,我有一个问题,我不知道迁移和种子数据库是否在laravel(php artisan)中工作,我有一个模型: use Illuminate\Auth\UserInterface; class User extends Eloquent implements UserInterface { public function getAuthIdentifier() { return $this->getKey(); } public function g
use Illuminate\Auth\UserInterface;
class User extends Eloquent implements UserInterface {
public function getAuthIdentifier() {
return $this->getKey();
}
public function getAuthPassword() {
return $this->password;
}
public function cats(){
return $this->hasMany('Cat');
}
public function owns(Cat $cat){
return $this->id == $cat->owner;
}
public function canEdit(Cat $cat){
return $this->is_admin or $this->owns($cat);
}
public function getRememberToken(){
return $this->remember_token;
}
public function setRememberToken($value){
$this->remember_token = $value;
}
public function getRememberTokenName(){
return 'remember_token';
}
}
这是我的迁移文件:
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUser extends Migration {
public function up()
{
Schema::create('users', function($table){
$table->increments('id');
$table->string('username');
$table->string('password');
$table->boolean('is_admin');
$table->timestamps();
});
Schema::table('cats', function($table){
$table->integer('user_id')->nullable()->references('id')->on('users');
});
}
public function down()
{
Schema::table('cats', function($table){
$table->dropForeign('cats_user_id_foreign');
$table->dropColumn('user_id');
});
Schema::drop('users');
}
}
最后,我的播种机代码:
class UsersTableSeeder extends Seeder {
public function run(){
DB::table('users')->delete();
User::create(array(
'username' =>'admin', 'password' =>Hash::make('hunter2'), 'is_admin' =>true
));
User::create(array(
'username'=>'scott', 'password' =>Hash::make('tiger'), 'is_admin' => false
));
}
}
当我尝试迁移此文件时,收到以下消息:
$php artisan migrate --path="foo"
Nothing to migrate
然后当我试图:
$php artisan db:seed
此消息显示给我:
[PDOException]
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel.users' doesn't exist
请帮帮我,伙计们,谢谢你们:)
和config/database.php:
return array(
'fetch' => PDO::FETCH_CLASS,
'default' => 'mysql',
'connections' => array(
'sqlite' => array(
'driver' => 'sqlite',
'database' => __DIR__.'/../database/production.sqlite',
'prefix' => '',
),
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'laravel',
'username' => 'root',
'password' => '2ez4rtz',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'pgsql' => array(
'driver' => 'pgsql',
'host' => 'localhost',
'database' => 'forge',
'username' => 'forge',
'password' => '',
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
),
'sqlsrv' => array(
'driver' => 'sqlsrv',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => '',
'prefix' => '',
),
),
'migrations' => 'migrations',
'redis' => array(
'cluster' => false,
'default' => array(
'host' => '127.0.0.1',
'port' => 6379,
'database' => 0,
),
),
);
在目录
foo
中,似乎没有任何迁移文件,或者您已经启动了一些迁移(是否使用来自2个目录的迁移?)
确保您有内部foo
迁移文件,并且之前没有运行任何迁移
编辑
如果您的迁移位于默认文件夹中,您只需运行:
php artisan迁移
不使用--path=“foo”
EDIT2
您可能应该更改:
$table->integer('user_id')->nullable()->references('id')->on('users');
进入:
可能是在您第一次尝试迁移时,它将迁移记录到了您的迁移表中。检查迁移表,让我知道您看到了什么。您能给我们看看您的
数据库.php
?(删除密码;)你能给我们看看你的文件夹结构吗?似乎artisan
@Jerodev在database\migration中找不到迁移文件我在database\seeds中有迁移文件我有种子代码,在模型中有模型代码在这种情况下,只需像@MarcinNabialek所说的那样运行php artisan migrate
。您不需要添加--path
参数。@Nabialek:我第一次在没有任何路径的情况下对php artsian进行treid迁移,然后运行并尝试迁移我的最后一个数据库,之后我尝试使用path@ArmanKuroKy您什么时候有迁移文件?你在foo
目录中有吗?文件名是什么?您是否已经在数据库中创建了migrations
表?@Nabialek让我先说一下,我使用这个命令“php artisan migrate:make create_users”,这将在迁移文件夹中为我创建一个2014_10_15_201401_create_user.php,然后我编写代码init,并使用“php artisan migrate--path=this file path”进行迁移,而不进行任何迁移appears@ArmanKuroKy如果文件是在默认迁移文件夹中创建的(这是app/database/migrations
您应该只运行php-artisan-migrate
,而不使用--path=“foo”
。所以试着运行php artisan migrate
@Nabialek我尝试了这个命令,但这个命令尝试迁移我的最后一个迁移文件,即2014年10月14日114343日添加猫和品种表。php在该文件夹中是的,如果你看到有人说你的用户表被迁移了,那么这意味着artisan认为它已经被迁移了,你知道吗解决方法是删除迁移表并重新运行所有迁移,迁移文件在哪里编辑:尝试运行composer dump autoload,然后尝试运行php artisan migrate AgainHP artsian migrate尝试迁移我的上一个文件而不是此文件,我不知道为什么,在此之后我尝试传递迁移路径它应该可以工作,唯一的我能想到的问题是,第一次迁移失败了,但还是记录了下来。检查mysql服务器中的每个数据库,并查找一个名为migrations的表。
$table->dropColumn('user_id');
$table->integer('user_id')->unsigned()->nullable();
$table->foreign('user_id')->references('id')->on('users');