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 call controller function from view in Laravel
- Laravel create multiple records in Pivot table
- How to pass query string to url in laravel
- Get laravel version
- Recursive function example code PHP Laravel
- How to Access Array in blade laravel
- Class 'App\Rules\Hash' not found in Laravel
- Create a record if not exist in laravel
- Pass value from controller to model in laravel
- The openssl extension is required for SSL/TLS protection but is not available
- Call to a member function update() on null
- Laravel create table migration with model
- Laravel onclick function not working
- How to display HTML tags In Laravel blade
- SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'user_id'
- How to get CSRF token in laravel controller
- How to create project_user pivot table in laravel
- Trying to get property 'title' of non-object
- Call to a member function pluck() on array
- Where to use whereNotNull eloquent in laravel
- Show old value while editing the form in Laravel
- How to get session in blade Laravel ?
- How to delete record in Laravel with ajax
- SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key
- Global scope in Laravel with example