title: Eloquent relationships (belongsTo, hasMany) with non-default primaryKey
body: <p>I'm new to relational databases, currently I am running a local sqlite database and simply trying to set up a a pivot/relational database where I can get a list of participants' messages by using their phone number as the primary key. There is no login, the login is essentially done by the user just by sending an SMS from their phone to the app, so I can't use typical 'id' column in my estimation. In my DB participant_id may as well be phone number. I am not sure what I am doing wrong.</p>
  
  <p>In my controller I simply try and DD the results using this code when I visit a predefined route:</p>
  
  <pre><code>public function showMessages()
  {
      $participant_id = "+5555555";
  
      //$activeParticipant = Participant::where('participant_id', $participant_id);
  
      $activeParticipant = Participant::find($participant_id);
      $messages = $activeParticipant-&gt;messages;
  
      dd($messages);
      //$messages = Participant::find($participant_id)-&gt;messages;
  }
  </code></pre>
  
  <p>I get the following error: </p>
  
  <p><code>SQLSTATE[HY000]: General error: 1 no such column: participants.id (SQL: select * from "participants" where "participants"."id" = +5555555 limit 1)</code></p>
  
  <p>Despite this error being present, I am pretty sure I have something wrong and I simply am not understanding what to do in the following code... if anyone could help explain this to me I would appreciate it.</p>
  
  <p>Here are my models and my database migrations:
  Participant migration:</p>
  
  <pre><code>class CreateParticipantsTable extends Migration
  {
  
  protected $primaryKey = 'participant_id';
  protected $incrementing = false;
  
  public function up()
  {
      Schema::create('participants', function (Blueprint $table) {
          $table-&gt;string('participant_id')-&gt;unique()-&gt;primary();
          $table-&gt;date('appointment_date')-&gt;nullable();
          $table-&gt;boolean('subscribed');
          $table-&gt;timestamps();
      });
  }
  </code></pre>
  
  <p>Participant model:</p>
  
  <pre><code>&lt;?php
  namespace App;
  use Illuminate\Database\Eloquent\Model;
  class Participant extends Model
  {
  protected $table = 'participants';
  
  protected $fillable = ['participant_id', 'appointment_date', 'subscribed'];
  
  public function messages()
  {
      return $this-&gt;hasMany(Message::class);
  //      return $this-&gt;hasMany('App\Message', 'message_id', 'participant_id');
  }
  public function notifications()
  {
      return $this-&gt;hasMany(Notification::class);
  }
  }
  </code></pre>
  
  <p>Message migration:</p>
  
  <pre><code>class CreateMessagesTable extends Migration
  {
  /**
   * Run the migrations.
   *
   * @return void
   */
  public function up()
  {
      Schema::create('messages', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;integer('message_id')-&gt;unsigned();
          $table-&gt;string('phoneNumber')-&gt;nullable();
          $table-&gt;string('message_content')-&gt;nullable();
          $table-&gt;string('mediaSID')-&gt;index()-&gt;nullable();
          $table-&gt;string('messageSID')-&gt;index()-&gt;nullable();
          $table-&gt;string('mediaURL')-&gt;index()-&gt;nullable();
          $table-&gt;binary('media')-&gt;nullable();
          $table-&gt;string('filename')-&gt;index()-&gt;nullable();
          $table-&gt;string('MIMEType')-&gt;nullable();
          $table-&gt;timestamps();
          $table-&gt;foreign('message_id')-&gt;references('participant_id')-&gt;on('participants')-&gt;onDelete('cascade');
      });
  }
  </code></pre>
  
  <p>Message model:</p>
  
  <pre><code>&lt;?php
  namespace App;
  use Illuminate\Database\Eloquent\Model;
  class Message extends Model
  {
  protected $table = 'messages';
  protected $hidden = 'media';
  protected $fillable = ['message_id', 'phoneNumber', 'message_content', 'mediaSID', 'messageSID', 'mediaURL', 'media', 'filename', 'MIMEType'];
  
  public function participant()
  {
      return $this-&gt;belongsTo(Participant::class);
  }
  }
  </code></pre>