<?php

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

class CreateCapsuleOpened extends Migration
{
    const TABLE_CAPSULE_OPEN = 'table_capsule_open';

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create(static::TABLE_CAPSULE_OPEN, function (Blueprint $table) {

            $table->increments('id');
            $table->unsignedInteger('user_id')->nullable();
            $table->unsignedInteger('capsule_id')->nullable();
            $table->unique(['user_id', 'capsule_id']);

            $table->timestamp('created_at');

            $table->foreign('user_id')->references('id')->on('table_user')->onDelete('cascade');
            $table->foreign('capsule_id')->references('id')->on('table_capsule')->onDelete('cascade');

            $table->index('created_at');
        });

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
}
