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

问题描述 投票:0回答:4

我正在尝试修改迁移

这里是迁移

<?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.

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

laravel
4个回答
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
投票

您的迁移语法错误

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

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

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

1
投票

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

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

然后只需运行一个工匠命令

php artisan migrate:fresh

0
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.