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 add background image to div using Tailwindcss, Vite in Laravel Environment
- PhpMyAdmin - Error The mysqli extension is missing
- Argument 1 passed to App\Http\Controllers\Auth\LoginController::authenticated() must be an instance of App\Http\Controllers\Auth\Request
- Json encode method in laravel
- Display success message in laravel
- Ajax GET request in laravel
- How to insert dynamic values to additional column with pivot column in pivot table on multiple records
- External link not working in laravel blade
- Class App\Http\Controllers\Admin\UserController Does Not Exist
- How to get route method name in Laravel
- Remove public from url in laravel project
- Target class [App\Http\Controllers\Auth\Request] does not exist.
- Rename Pivot Table in Laravel
- Get only 10 records from table in laravel
- Method chaining in Laravel
- How to get tomorrow and yesterday date in laravel
- Laravel migration add foreign key to existing table
- How to delete record in Laravel with ajax
- How to display order by null last in laravel
- Undefined property: stdClass::$title
- How to create laravel project using composer
- Create project table with model and migration
- Sample .htaccess file and index.php file under public directory in laravel
- Conditional validation in laravel
- Add class to body in laravel view