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条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改