Created
June 17, 2020 08:26
-
-
Save amrography/c6290ca07ca6708a82aadc2da2fb07f4 to your computer and use it in GitHub Desktop.
Create foreign keys in laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Database\Migrations\Migration; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Support\Facades\Schema; | |
class AsssignProductsForeignKeys extends Migration | |
{ | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::table('prodcuts', function (Blueprint $table) { | |
$table->unsignedInteger('category_id')->nullable(); | |
$table->foreign('category_id')->references('id')->on('categories'); | |
}); | |
Schema::table('prodcuts', function (Blueprint $table) { | |
$table->unsignedInteger('type_id')->nullable(); | |
$table->foreign('type_id')->references('id')->on('types'); | |
}); | |
Schema::table('prodcuts', function (Blueprint $table) { | |
$table->unsignedInteger('attribute_id')->nullable(); | |
$table->foreign('attribute_id')->references('id')->on('attributes'); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
* | |
* @return void | |
*/ | |
public function down() | |
{ | |
// DO NOT DROP TABLE HERE | |
// ONLY, DELETE OR UPDATE COLUMNS HERE | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment