Php 未找到Laravel 4迁移基表
我正在尝试制作以下教程: 说到跑步:Php 未找到Laravel 4迁移基表,php,mysql,laravel,laravel-4,Php,Mysql,Laravel,Laravel 4,我正在尝试制作以下教程: 说到跑步: php artisan migrate 我发现以下错误: [Exception] SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel.user' doesn't exist (SQL: alter table `user` ad
php artisan migrate
我发现以下错误:
[Exception]
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel.user' doesn't
exist (SQL: alter table `user` add `id` int unsigned not null auto_increment prim
ary key, add `username` varchar(255) null, add `password` varchar(255) null, add
`email` varchar(255) null, add `created_at` datetime null, add `updated_at` datet
ime null) (Bindings: array (
))
数据库连接正在工作,迁移表已成功创建。如错误消息中所示,数据库名称已更改
对我来说很奇怪的是,它试图改变这个不存在的表,而不是创建它
这里是我的其他文件,比如UserModel、Seeder、migation和DB Config
CreateUserTable:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUserTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('user', function(Blueprint $table)
{
$table->increments("id");
$table
->string("username")
->nullable()
->default(null);
$table
->string("password")
->nullable()
->default(null);
$table
->string("email")
->nullable()
->default(null);
$table
->dateTime("created_at")
->nullable()
->default(null);
$table
->dateTime("updated_at")
->nullable()
->default(null);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('user', function(Blueprint $table)
{
Schema::dropIfExists("user");
});
}
}
用户播种器:
class UserSeeder extends DatabaseSeeder
{
public function run()
{
$users = [
[
"username" => "ihkawiss",
"password" => Hash::make("123456"),
"email" => "ihkawiss@domain.com"
]
];
foreach ($users as $user)
{
User::create($user);
}
}
}
数据库播种机:
class DatabaseSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Eloquent::unguard();
$this->call('UserSeeder');
}
}
数据库配置:
return array(
/*
|--------------------------------------------------------------------------
| PDO Fetch Style
|--------------------------------------------------------------------------
|
| By default, database results will be returned as instances of the PHP
| stdClass object; however, you may desire to retrieve records in an
| array format for simplicity. Here you can tweak the fetch style.
|
*/
'fetch' => PDO::FETCH_CLASS,
/*
|--------------------------------------------------------------------------
| Default Database Connection Name
|--------------------------------------------------------------------------
|
| Here you may specify which of the database connections below you wish
| to use as your default connection for all database work. Of course
| you may use many connections at once using the Database library.
|
*/
'default' => 'mysql',
/*
|--------------------------------------------------------------------------
| Database Connections
|--------------------------------------------------------------------------
|
| Here are each of the database connections setup for your application.
| Of course, examples of configuring each database platform that is
| supported by Laravel is shown below to make development simple.
|
|
| All database work in Laravel is done through the PHP PDO facilities
| so make sure you have the driver for your particular database of
| choice installed on your machine before you begin development.
|
*/
'connections' => array(
'sqlite' => array(
'driver' => 'sqlite',
'database' => __DIR__.'/../database/production.sqlite',
'prefix' => '',
),
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'laravel',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'pgsql' => array(
'driver' => 'pgsql',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
),
'sqlsrv' => array(
'driver' => 'sqlsrv',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => '',
'prefix' => '',
),
),
/*
|--------------------------------------------------------------------------
| Migration Repository Table
|--------------------------------------------------------------------------
|
| This table keeps track of all the migrations that have already run for
| your application. Using this information, we can determine which of
| the migrations on disk have not actually be run in the databases.
|
*/
'migrations' => 'migrations',
/*
|--------------------------------------------------------------------------
| Redis Databases
|--------------------------------------------------------------------------
|
| Redis is an open source, fast, and advanced key-value store that also
| provides a richer set of commands than a typical key-value systems
| such as APC or Memcached. Laravel makes it easy to dig right in.
|
*/
'redis' => array(
'cluster' => true,
'default' => array(
'host' => '127.0.0.1',
'port' => 6379,
'database' => 0,
),
),
);
希望有人能给我一个提示
在您的
CreateUserTable
迁移文件中,最好使用ihkawiss,而不是Schema::table
,您必须使用Schema::create
Schema::table
用于更改现有表,而Schema::create
用于创建新表
检查文档:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUserTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('user', function(Blueprint $table) {
{
$table->increments("id",true);
$table->string("username")->nullable()->default(null);
$table->string("password")->nullable()->default(null);
$table->string("email")->nullable()->default(null);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists("user");
}
}
这可能是因为用于创建迁移的语法不太正确。在这种情况下,--create
将无法正确拾取,您将看到Schema::table
,而不是预期的Schema::create
。当像这样生成迁移文件时,您可能还缺少一些用于创建迁移的其他标记,例如$table->increments('id')代码>和$table->timestamps()代码>
例如,这两个命令不会像您预期的那样创建create table迁移文件:
php artisan migrate:make create_tasks_table --table="tasks" --create
php artisan migrate:make create_tasks2_table --table=tasks2 --create
但是,该命令不会因错误而失败。相反,laravel将使用Schema::table
创建新迁移文件时,我总是使用完整语法,如下所示:
php artisan migrate:make create_tasks_table --table=tasks --create=tasks
避免任何类似的问题 有时这是由自定义artisan命令引起的。其中一些命令可能会启动几个类。因为我们进行了回滚,所以找不到表。选中“自定义artisan命令”。您可以注释掉导致触发的行。运行php artisan migrate命令,然后取消注释。这就是我必须要做的。我现在已经设法让它工作了,但不幸的是,这只是一个变通办法。我创建了一个表用户,添加了一个不是由迁移创建的列,运行php artisan migrate。在那之后,我删除了创建的专栏。我偶然发现了同样的事情。迁移:create使用“Schema::table”,需要将其更改为“Schema:create”。我认为这些模板可以在某处修改。谢谢!!为我工作!!
php artisan migrate:make create_tasks_table --table=tasks --create=tasks