Search code examples
phpmysqllaravelcomposite-key

How to create composite key in Laravel?


I tried many ways but nothing work. I need to add a composite key to my table daily_deals_products. Which is developed in laravel and mysql.

<?php

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

class CreateDailyDealsProductsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('daily__deals__products', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('product_id');
            $table->timestamps();

      
        });
    }

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

Solution

  • Schema::create("daily__deals__products", function($table) {
        $table->increments('id');
        $table->integer('product_id');
        $table->string('name');
    });
    
    DB::unprepared('ALTER TABLE `daily__deals__products` DROP PRIMARY KEY, ADD PRIMARY KEY (  `id` ,  `product_id` )');