drop foreign key column in laravel migration code example
Example 1: larael drop foreign key
Schema::table('posts', function (Blueprint $table) {
$table->dropForeign(['category_id']);
});
Example 2: laravel foreign key
Schema::table('posts', function (Blueprint $table) {
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
});
OR
Schema::table('posts', function (Blueprint $table) {
$table->foreignId('user_id')->constrained();
});
Example 3: laravel remove foreign key
$table->dropForeign('posts_user_id_foreign');
Example 4: laravel 8 foreign key migration
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Schema::table('posts', function (Blueprint $table) {
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
});
Example 5: laravel drop foreign column
Schema::table('users', function (Blueprint $table) {
$table->dropColumn(['votes', 'avatar', 'location']);
});
Example 6: laravel migration drop foreign keys
$table->dropPrimary('users_id_primary');