php - 如何使用 laravel 迁移

标签 php laravel migration

我正在使用 php 的 Laravel 框架进行开发。我想使用迁移来创建表操作。这些是我采取的步骤:

  1. 我使用命令 php artisan migrate:make create_users_table 创建迁移,它创建了一个迁移文件,在它的 up 函数中,我写下了我的架构,然后运行它,它成功了执行。

  2. 之后,我再次尝试运行相同的迁移,结果显示错误“表存在”。

  3. 然后我尝试使用回滚函数,但它给出错误“没有什么可以回滚”。

    那么,如何回滚该迁移或执行迁移的向下功能。此外,当我创建新的迁移并在迁移文件的 up 函数中,我编写了用于删除由我之前的迁移创建的表的代码,并使用命令 php artisan migrate 执行,由此所有迁移都被执行(也是我的较早的一个)并向我显示错误“表已存在”(很明显)。

所以,现在我卡住了,是否有执行特殊/特定迁移的功能?我该怎么做?

最佳答案

当您使用 artisan migrate:make 创建迁移时,您应该编写 updown 方法。 down 方法应该执行与 up 方法相反的操作。

public function up()
{
    Schema::create('users', function($table)
    {
        $table->increments('id');
        $table->string('username');
    });
}

public function down()
{
    // We DROP the table because we created it in the "up" method.
    Schema::drop('users');
}

在我看来你的 up 方法中有一些不属于那里的代码,尽管如果没有看到你的代码就很难说。我建议您清除我们的 migrations 表(可能有也可能没有任何记录)。您还需要手动删除通过迁移创建的表。然后您可以重新开始。

请记住,您也可以使用 dropIfExists 仅在表存在时删除它。

public function down()
{
    // Drop the table only if it exists.
    Schema::dropIfExists('users');
}

关于php - 如何使用 laravel 迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23010146/

相关文章:

php - 使用 AJAX 在 Laravel 中将项目添加到数据库,然后显示项目

laravel - curl 错误 60 : SSL certificate problem: unable to get local issuer certificate (see https://curl. haxx.se/libcurl/c/libcurl-errors.html)

ruby-on-rails - 如何让 Rails 迁移在 Heroku 上自动运行

c# - 我的 C# Entity Frameworks 项目闹鬼了吗?

javascript - 来自 React 的 PHP 外部变量

php - Paypal - 传递多个 'Custom' 变量

mysql - Laravel - 数据库事务 - 超出锁定等待超时

laravel - 如何获得其他用户撰写的用户提交的评价

c# - Visual Studio 2010 项目转换为 Visual Studio 2012

php - php 中带有超时参数的 exec() ?