title: Custom Primary Key in laravel
body: <p>I created a class:</p>
  
  <pre><code>class Incident extends Model {
  
      use EventGenerator;
  
      protected $table = 'incidents';
      protected $primaryKey = "incident_id";
      protected $fillable = ['incident_id', 'incident_type', 'location', 'street', 'city', 'latitude', 'longitude', 'date', 'time', 'incident_archived'];
      public $timestamps = true;
  
      public function set($data) {
          foreach ($data as $key =&gt; $value) {
              $this-&gt;{$key} = $value;
          }
          return $this;
      }
  
      public function responders() {
          return $this-&gt;hasMany('App\Classes\Responder');
      }
  
      public function incidentTypes() {
          return $this-&gt;belongsToMany('App\IncidentType', 'incident_incident_type', 'incident_id');
      }
  
      // set fields on the eloquent object and save to database
      // raise event that the incident was created.
      public function createIncident($command) {
          $this-&gt;incident_id = $command-&gt;incidentId;
          $this-&gt;save();
          $this-&gt;raise(new IncidentWasPosted($this));
          return $this;
      }
  }
  </code></pre>
  
  <p>The primary key should be a custom one <code>incident_id</code>. Thats why I set the property. Still when I use tinker to create an object like this:</p>
  
  <pre><code>$incident = new \App\Incident
  $incident-&gt;incident_id = "I100"
  $incident-&gt;save()
  $incident-&gt;incidentTypes()-&gt;attach("AED")
  </code></pre>
  
  <p>and want to use the attach() method, I get the error:</p>
  
  <blockquote>
    <p>Illuminate\Database\QueryException with message 'SQLSTATE[23000]:
    Integrity constraint violation: 1452 Cannot add or update a child row:
    a foreign key constraint fails (<code>homestead</code>.<code>incident_incident_type</code>,
    CONSTRAINT <code>incident_incident_type_incident_id_foreign</code> FOREIGN KEY
    (<code>incident_id</code>) REFERENCES <code>incidents</code> (<code>incident_id</code>)) (SQL: insert
    into <code>incident_incident_type</code> (<code>incident_id</code>, <code>incident_type_id</code>)
    values (0, AED))'</p>
  </blockquote>
  
  <p><strong>Question</strong></p>
  
  <p>Why is the value being added to the SQL statement still "0" and not "I100" like I added it? as this is the valid PK and not 0 (which would be the default "id" field as a PK.</p>