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!.
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']);
});
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