<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateBranchMembersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('branch_members', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('branch_id');
            $table->unsignedInteger('member_id');
            $table->unique([
                'branch_id',
                'member_id',
            ], 'branch_id_member_id_unique');
        });

        Schema::table('branch_members', function (Blueprint $table) {
            $table->foreign('branch_id')->references('id')->on('branches')->onDelete('cascade');
            $table->foreign('member_id')->references('id')->on('members')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('branch_members', function (Blueprint $table) {
            $table->dropForeign('branch_members_branch_id_foreign');
            $table->dropForeign('branch_members_member_id_foreign');
        });

        Schema::dropIfExists('branch_members');
    }
}
