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

调用未定义的方法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!.

图片转代码服务由CSDN问答提供 功能建议

 类CreateCommentsTable扩展Migration 
 {
 / ** 
 *运行迁移。
  * 
 * @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(); 
}); 
 \  n Schema :: table('comments',function(Blueprint $ table){
 $ table-> foreign('post_id') - > references('id') - > on('posts'); \  n}); 
} 
 
 / ** 
 *反转迁移。
 * 
 * @return void 
 * / 
公共函数down()
 
 
 
 \ {
 
 
 
 
 
 
 
 
架构:: dropForeign  (['post_id']); 
 Schema :: dropIfExists('comments'); 
} 
} 
   
 
 

这是我的迁移类看起来的样子,我一直在尝试从数据库中删除该表,但它会引发错误。

错误

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

我经历过 文档,但它似乎没有多大帮助。

任何人都可以指出我的错误和解决方案是什么?

就这样 你知道,我是laravel的新手。对我很轻松。谢谢!。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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']);
        });
    
    点赞 打赏 评论
  • dongtong5242 2017-01-01 16:41

    You're using wrong syntax. It should be:

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

    https://laravel.com/docs/5.3/migrations#foreign-key-constraints

    点赞 打赏 评论

相关推荐 更多相似问题