Skip to content

Instantly share code, notes, and snippets.

@nextlevelshit
Last active February 17, 2018 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nextlevelshit/932e5ef7c927ecdbc390665e51724e20 to your computer and use it in GitHub Desktop.
Save nextlevelshit/932e5ef7c927ecdbc390665e51724e20 to your computer and use it in GitHub Desktop.
Laravel 5 many-to-many migration table for belongsToMany() connections between two Entities. This is a so called pivot table two foreign keys, that create its index.
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAuthorEntryTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('author_entry', function (Blueprint $table) {
$table->integer('author_id')->unsigned()->index();
$table->foreign('author_id')->references('id')
->on('authors')->onDelete('cascade');
$table->integer('entry_id')->unsigned()->index();
$table->foreign('entry_id')->references('id')
->on('entries')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('author_entry');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment