Search code examples
eloquentlaravel-8laravel-9

Unable to create multiple primary attributes


How to define this relationship in Laravel ? I tried to develop three primary keys in Laravel. but it doesn't work. how could i resolve this ?

enter image description here


Solution

  • Those Keys may symbolise primary keys, foreign keys, unique columns or indexes.

    You cannot have more than one primary key on a table.

    Here is what I tried: Diagram

    php .\artisan make:model Employee -m
    php .\artisan make:model Title -m
    php .\artisan make:model Salary -m
    
    class CreateEmployeesTable extends Migration
    {
        public function up(): void
        {
            Schema::create('employees', function (Blueprint $table) {
                $table->bigIncrements('emp_no');
                $table->string('first_name', 14);
                $table->string('last_name', 16);
                $table->date('birth_date');
                $table->date('hire_date');
            });
        }
    };
    
    class CreateTitlesTable extends Migration
    {
        public function up(): void
        {
            Schema::create('titles', function (Blueprint $table) {
                $table->unsignedBigInteger('emp_no');
                $table->string('title', 50)->index();
                $table->date('from_date')->index();
    
                $table->foreign('emp_no')->references('emp_no')->on('employees');
            });
        }
    };
    
    class CreateSalariesTable extends Migration
    {
        public function up(): void
        {
            Schema::create('salaries', function (Blueprint $table) {
                $table->unsignedBigInteger('emp_no');
                $table->integer('salary');
                $table->date('from_date')->index();
    
                $table->foreign('emp_no')->references('emp_no')->on('employees');
            });
        }
    };
    

    Edit

    Assuming that multiple key icons mean a composite key

    class CreateEmployeesTable extends Migration
    {
        public function up(): void
        {
            Schema::create('employees', function (Blueprint $table) {
                $table->bigIncrements('emp_no');
                $table->string('first_name', 14);
                $table->string('last_name', 16);
                $table->date('birth_date');
                $table->date('hire_date');
            });
        }
    };
    
    class CreateTitlesTable extends Migration
    {
        public function up(): void
        {
            Schema::create('titles', function (Blueprint $table) {
                $table->unsignedBigInteger('emp_no');
                $table->string('title', 50);
                $table->date('from_date');
    
                $table->foreign('emp_no')->references('emp_no')->on('employees');
                $table->primary(['emp_no', 'title', 'from_date']);
            });
        }
    };
    
    class CreateSalariesTable extends Migration
    {
        public function up(): void
        {
            Schema::create('salaries', function (Blueprint $table) {
                $table->unsignedBigInteger('emp_no');
                $table->integer('salary');
                $table->date('from_date');
    
                $table->foreign('emp_no')->references('emp_no')->on('employees');
                $table->primary(['emp_no', 'from_date']);
            });
        }
    };