dqmq0654 2017-01-01 16:27
浏览 414
已采纳

调用未定义的方法Illuminate \ Database \ Schema \ MySqlBuilder :: dropForeign()

class CreateCommentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('comments', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email');
            $table->text('comment');

            $table->boolean('approved');
            $table->integer('post_id')->unsigned();
            $table->timestamps();
        });

        Schema::table('comments', function (Blueprint $table) {
            $table->foreign('post_id')->references('id')->on('posts');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropForeign(['post_id']);
        Schema::dropIfExists('comments');
    }
}

This is what my migration class looks like, i have been trying to delete the table from the database but it throws me an error.

Error

Call to undefined method Illuminate\Database\Schema\MySqlBuilder::dropForeign()

I have gone through the documentation but it doesnot seem to be of much help.

could anyone please point out my mistake and what would be the solution ?

Just so you know , i am new to laravel.Go easy on me.Thanks!.

  • 写回答

2条回答 默认 最新

  • douye2572 2017-01-01 16:41
    关注

    dropForeign needs to be called under Schema::table with a Blueprint object,

    Schema::table('comments', function (Blueprint $table) {
            $table->dropForeign('comments_post_id_foreign');
        });
    

    This follows the naming convention of <table_name>_<foreign_table_name>_<column_name>_foreign.

    OR

    Schema::table('comments', function (Blueprint $table) {
            $table->dropForeign(['your_key_name']);
        });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀