
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 create belongstomany relation using custom name on custom pivot table
- How to get selected categories on edit record with Select2
- Add [name] to fillable property to allow mass assignment on [App\Models\Project]
- RuntimeException You must enable the openssl extension in your php.ini to load information from https://repo.packagist.org
- Touch parent updated_at in Laravel
- Non-static method App\Http\Helper::myFunction() should not be called statically
- How to increment column value of table in Laravel
- Session Doesn't Work on Redirect
- Pagination in laravel
- Undefined property: stdClass::$title
- Rename Pivot Table in Laravel
- Conditional where clause in Laravel
- Connection could not be established with host smtp.gmail.com :stream_socket_client(): unable to connect to tcp://smtp.gmail.com:587 (Connection refused)"
- How to get last month records in Laravel
- Count all and get 10 records after where condition in laravel
- Laravel 9 pagination with search filter
- Skip first n record and display rest records in laravel view
- Display message with session flash using bootstrap alert class in laravel
- Insert dummy data in users table Laravel
- Laravel create multiple records in Pivot table
- Get count of filter data, while return a small set of records
- Validation for multiple forms on same page in laravel
- Update email with unique validation in laravel
- Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
- Use withCount() to Calculate Child Relationship Records