如何在laravel框架中编辑迁移?

时间:2019-10-06 12:12:51

标签: laravel

我正在尝试修改迁移

这是迁移

<?php

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

class CreateCustomersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('customers.blade.php', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('customers.blade.php');
    }
}

我已经添加了电子邮件部分,但是当我尝试使用回滚时,出现此错误

Rolling back: 2019_10_06_090218_create_customers_table

   Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 1 near ".": syntax error (SQL: drop table if exists "customers"."blade"."php")

  at /Users/macair13/blog/meatrack/vendor/laravel/framework/src/Illuminate/Database/Connection.php:665
    661|         // If an exception occurs when attempting to run a query, we'll format the error
    662|         // message to include the bindings with SQL, which will make this exception a
    663|         // lot more helpful to the developer instead of just the database's errors.
    664|         catch (Exception $e) {
  > 665|             throw new QueryException(
    666|                 $query, $this->prepareBindings($bindings), $e
    667|             );
    668|         }
    669| 

  Exception trace:

  1   PDOException::("SQLSTATE[HY000]: General error: 1 near ".": syntax error")
      /Users/macair13/blog/meatrack/vendor/laravel/framework/src/Illuminate/Database/Connection.php:453

  2   PDO::prepare("drop table if exists "customers"."blade"."php"")
      /Users/macair13/blog/meatrack/vendor/laravel/framework/src/Illuminate/Database/Connection.php:453

  Please use the argument -v to see more details.

请帮助我解决此问题,因为对于我自己,我看不到自己在做什么。

4 个答案:

答案 0 :(得分:1)

传递表名而不是视图名

<?php

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

class CreateCustomersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // Here
        Schema::create('customers', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        // And here
        Schema::dropIfExists('customers');
    }
}

name.blade.php保留用于HTML视图

答案 1 :(得分:1)

您的迁移语法错误

在“向上”处理程序中添加一列时,建议在“向下”处理函数中将其删除。

在您的情况下,您确实添加了“名称”列”,并且向下将删除整个表(尽管格式错误)!

public function down()
{
    $table->dropColumn('name');
}

答案 2 :(得分:1)

您在架构上的表名错误,请替换为以下内容

public function up()
    {
        Schema::create('customers', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->timestamps();
        });
    }

然后只运行一个工匠命令

php artisan migrate:fresh

答案 3 :(得分:0)

您不应修改/回滚已部署的迁移。

如果它仍然仅在开发数据库中,则只需重置数据库即可。 (php artisan migrate:fresh

...就像其他答案中已经提到的那样,您可能使用了错误的表名。迁移与您的视图(HTML模板)文件名无关。