PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, November 15, 2022

[FIXED] How to create composite key in ORM Eloquent Laravel 6.2

 November 15, 2022     eloquent, laravel, laravel-6.2, php     No comments   

Issue

I would like create a composite key between two table in my database. I have find how to create that in migration but for Eloquente ORM I have found nothing.

So here is my migration :

Schema::create('passages', function (Blueprint $table) {
            $table->unsignedBigInteger('id_contract');
            $table->integer('month');
            $table->integer('year');
            $table->text('object');
            $table->integer('nbPassage');
            $table->decimal('priceExclTax');
            $table->timestamps();

            $table->foreign('id_contract')->references('id')->on('contracts');
            $table->primary(['id_contract','mois','annee']);
});

So I have try to make in model a belong relationship, but when i try to save my object nothing append, and I don't have anny row in my DataBase. So I think this is the wrong way.

If you have an idea please help me ^^.


Solution

As per laravel, Eloquent does not support composite primary keys. You can check this issue on laravel github repository here

If you really want to do this then you can override setKeysForSaveQuery of Model.php and set up your keys.

By Default:

protected function setKeysForSaveQuery(Builder $query)
{
    $query->where($this->getKeyName(), '=', $this->getKeyForSaveQuery());

    return $query;
}

$this->getKeyName() will return the primary key name and $this->getKeyForSaveQuery() will return the value of the key.

This means that Eloquent always filter with only 1 field.

This which will be called once you perform a save action (UPDATE and DELETE) and what it does is that it generates a WHERE clause in the SQL.

for e.g. Put this into that model that uses a composite primary key:

   protected function setKeysForSaveQuery(Builder $query)
    {
        $keys = $this->getKeyName();
        if(!is_array($keys)){
            return parent::setKeysForSaveQuery($query);
        }

        foreach($keys as $keyName){
            $query->where($keyName, '=', $this->getKeyForSaveQuery($keyName));
        }

        return $query;
    }

    
    protected function getKeyForSaveQuery($keyName = null)
    {
        if(is_null($keyName)){
            $keyName = $this->getKeyName();
        }

        if (isset($this->original[$keyName])) {
            return $this->original[$keyName];
        }

        return $this->getAttribute($keyName);
    }

If you use the Builder argument type in your setKeysForSaveQuery() definition then you'll also need to add the following to the top of the model:

use Illuminate\Database\Eloquent\Builder;


Answered By - Sehdev
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

1,209,786

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © 2025 PHPFixing