
laravel migration add foreign key to existing table
laravel migration add foreign key to existing table
In this session, we’ll try our hand at solving the "laravel migration add foreign key to existing table" puzzle by using the computer language.
You can add a foreign key to the existing table in laravel using migration. Laravel provides migration column type foreginId() which helps you to create a foreign key in an already existing table in your database.-
Create foreign key to an existing table in laravel 8
//Create migration file php artisan make:migration add_user_id_to_posts_table --table=posts //Create table structure using column type in the generated migration file //database\migrations\<2021_07_26_032616>_add_user_id_to_posts_table.php public function up() { Schema::table('posts', function (Blueprint $table) { $table->foreignId('user_id')->constrained(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('posts', function (Blueprint $table) { $table->dropForeign(['user_id']); }); } //Run artisan command to create a foreign key php artisan migrate
Laravel 8 has a foreignId() column type in migration schema builder. Which is used to create a foreign key in laravel with an UNSIGNED BIGINT equivalent column.
Related Queries
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 upload files to amazon s3 bucket using Laravel
- Laravel 11 step by step instructions to upload file in storage directory and display in blade file
- Call to a member function getRelationExistenceQuery() on array in Laravel
- Use withCount() to Calculate Child Relationship Records
- If no route matched route::fallback in laravel
- Class App\Http\Controllers\Admin\UserController Does Not Exist
- Calculate age from date of birth in Laravel
- Property [user] does not exist on this collection instance
- Pagination in laravel
- Route group with URI prefix using middleware and route name prefixes
- How to add a key value pair to existing array in laravel
- Laravel upload file with original file name
- Create record with unique slug in laravel
- Laravel get count with where condition
- Permission denied error while creating storage link in Laravel
- Get products with number of orders in Laravel
- Laravel 10 Breeze Authentication Example
- How to Run CRON Job on LIVE SERVER on Cpanel in Laravel Project
- How to run a specific seeder class in laravel
- Get latest record by created at in Laravel
- How to get route name on visit URL in laravel
- Display first n record from collection in laravel view
- SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'posts_user_id_foreign'; check that column/key exists
- Sample configuration files to create laravel project with docker using wsl (window subsystem linux)
- How to call Laravel route in jQuery