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');
}
}
这是我的迁移类的样子,我一直在尝试从数据库中删除表,但给我带来了一个错误。
错误
致电未定义的方法照明 database schema mysqlbuilder :: dropforeign()
我已经完成了文档,但这似乎没有太大帮助。
任何人都可以指出我的错误,解决方案是什么?
您知道,我是Laravel的新手。
dropForeign
需要在Schema::table
下使用Blueprint
对象,
Schema::table('comments', function (Blueprint $table) {
$table->dropForeign('comments_post_id_foreign');
});
这遵循<table_name>_<foreign_table_name>_<column_name>_foreign
的命名约定。
或
Schema::table('comments', function (Blueprint $table) {
$table->dropForeign(['your_key_name']);
});
您正在使用错误的语法。应该是:
Schema::table('comments', function (Blueprint $table) {
$table->dropForeign('comments_post_id_foreign');
});
https://laravel.com/docs/5.3/migrations#foreign-key-constraints