<?php

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

class CreateCourseStudentTable extends Migration
{

	/**
	 * Run the migrations.
	 *
	 * @return void
	 */
	public function up()
	{
		Schema::create('course_student', function (Blueprint $table) {
			$table->increments('id');
			$table->integer('course_id')->unsigned();
			$table->integer('student_id')->unsigned();
			$table->timestamps();
		});

		Schema::table('course_student', function (Blueprint $table) {
			$table->foreign('course_id')->references('id')->on('courses')->onDelete('cascade')->onUpdate('cascade');
			$table->foreign('student_id')->references('id')->on('students')->onDelete('cascade')->onUpdate('cascade');
		});
		Schema::table('course_student', function (Blueprint $table) {
			$table->unique(array('course_id', 'student_id'));
		});
		Schema::table('course_student', function (Blueprint $table) {
			$table->integer('semester_id')->unsigned()->default(1)->after('student_id');
		});

		Schema::table('course_student', function (Blueprint $table) {
			$table->foreign('semester_id')->references('id')->on('semesters');
		});
	}

	/**
	 * Reverse the migrations.
	 *
	 * @return void
	 */
	public function down()
	{
		Schema::drop('course_student');
	}
}
