Create Pivot table (Migration) with Laravel 5

Submitted by Mahmoud_Zalt - 8 years ago

Migration file sample for Pivot table

For Many to Many relations:



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

class CreateAccountTagTable extends Migration
{

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('account_tag', function (Blueprint $table) {
            $table->integer('account_id')->unsigned()->index();
            $table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade');

            $table->integer('tag_id')->unsigned()->index();
            $table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');

            $table->timestamps();
        });
    }

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



Note: in case you have timestamps on the pivot table you must set withTimestamps on the relationship of both ends

return $this->belongsToMany(\Mega\Modules\Account\Models\Tag::class)->withTimestamps();

return $this->belongsToMany(\Mega\Modules\Account\Models\Account::class)->withTimestamps();