我有这个问题,我不知道在 laravel (php artisan) 中工作的迁移和种子数据库,我有这个模型:
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,
),
),
);