Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save amrography/c6290ca07ca6708a82aadc2da2fb07f4 to your computer and use it in GitHub Desktop.
Save amrography/c6290ca07ca6708a82aadc2da2fb07f4 to your computer and use it in GitHub Desktop.
Create foreign keys in laravel
<?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