Drop foreign key column in Laravel using migration
Drop foreign key column in Laravel using migration
We’ll attempt to use programming in this lesson to solve the "Drop foreign key column in Laravel using migration" puzzle.
You can drop foreign key column with constraints in Laravel using migration, you have to create migration file and add dropForeign() and dropColumn() method inside up or down function in migration file and run migration command to drop the column.-
Drop foreign key constraint posts_user_id_foreign from posts table with column in Laravel using migration
--PATH database\migrations\<2021_11_15_101237_drop_posts_user_id_foreign_from_posts>.phppublic function up(){ Schema::table('posts', function(Blueprint $table){ $table->dropForeign('posts_user_id_foreign'); $table->dropColumn('user_id'); }); }
To get the foreign key constraint, click on table in the specific database and click on relation view after click on structure of the table.
Create migration file using php artisan make:migration drop_posts_user_id_foreign_from_posts --table=posts command.
Add the foreign key constraint to the dropForeign method which you want to drop and run migration command to drop the column and foreign key constraint.
-
Drop foreign key constraint with index and column in Laravel using migration
--PATH database\migrations\<2021_11_15_101237_drop_posts_user_id_foreign_from_posts>.phppublic function up(){ Schema::table('posts', function(Blueprint $table){ $table->dropForeign('posts_user_id_foreign'); $table->dropIndex('posts_user_id_foreign'); $table->dropColumn('user_id'); }); }
Using this code snippet you can drop foreign key column with index and constraint name in Laravel.
To get the foreign key constraint, click on table in the specific database and click on relation view after click on structure of the table.
Create migration file using php artisan make:migration drop_posts_user_id_foreign_from_posts --table=posts command.
Add the foreign key constraint to the dropForeign method which you want to drop and run migration command to drop the column and foreign key constraint.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- How to display validation error in laravel
- How to check relationship is loaded or not in Laravel
- Insert Comma Separated Values in laravel
- How to check data inserted or deleted in pivot after toggle method
- Rename Pivot Table in Laravel
- How to run a specific seeder class in laravel
- Validation errors for multiple forms on same page Laravel
- Class 'App\Providers\Auth' not found
- Insert current date time in a column using Laravel
- How to insert dynamic value to additional column in pivot table in laravel
- Syntax error or access violation: 1072 Key column 'role_id' doesn't exist in table (SQL: alter table `users` add constraint `users_role_id_foreign` foreign key (`role_id`) references `roles` (`id`))
- SQLSTATE[23000]: Integrity constraint violation: 1022 Can't write; duplicate key in table
- How to add foreign key in laravel using migration
- Route [password.request] not defined
- Laravel 11 project setup on localhost using breeze with blade step by step
- How to get specific columns using Laravel eloquent methods
- Display option of select as selected with blade directive Laravel
- Create a record if not exist in laravel
- Get Array of IDs from Eloquent Collection
- Get all users except the followings users in overtrue laravel-follow
- Get ids in array from users table
- How to get list of all views file in laravel
- How to avoid duplicate entries in pivot table in Laravel
- The openssl extension is required for SSL/TLS protection but is not available
- Method Illuminate\Database\Eloquent\Collection::lists does not exist