title: Eloquent Multiple Foreign Keys from same table in two columns
body: <p>I am having issues calling a departure ICAO and arrival ICAO from my schedules table. Laravel keeps giving me errors that my relationships are screwed up. Here is the code.</p>
  
  <pre><code>Schema::create('airports', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;string('name');
          $table-&gt;string('city');
          $table-&gt;string('country');
          $table-&gt;string('iata');
          $table-&gt;string('icao');
          $table-&gt;double('lat');
          $table-&gt;double('lon');
          $table-&gt;longText('data')-&gt;nullable(); //JSON Data for All gate information for the system.
          $table-&gt;softDeletes();
      });
  Schema::create('schedule_templates', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;string('code');
          $table-&gt;string('flightnum');
          $table-&gt;integer('depicao')-&gt;unsigned();
          $table-&gt;foreign('depicao')-&gt;references('id')-&gt;on('airports')-&gt;onDelete('cascade');
          $table-&gt;integer('arricao')-&gt;unsigned();
          $table-&gt;foreign('arricao')-&gt;references('id')-&gt;on('airports')-&gt;onDelete('cascade');
          $table-&gt;string('aircraft')-&gt;nullable();
          $table-&gt;boolean('seasonal');
          $table-&gt;date('startdate');
          $table-&gt;date('enddate');
          $table-&gt;time('deptime');
          $table-&gt;time('arrtime');
          $table-&gt;integer('type');
          $table-&gt;boolean('enabled');
          $table-&gt;text('defaults');
          $table-&gt;timestamps();
          $table-&gt;softDeletes();
      });
  </code></pre>
  
  <p>Here are the Models</p>
  
  <pre><code>class ScheduleTemplate extends Model
  {
      public $table = "schedule_templates";
  
      public function depicao()
      {
          return $this-&gt;hasOne('App\Models\Airport', 'depicao');
      }
      public function arricao()
      {
          return $this-&gt;hasOne('App\Models\Airport', 'arricao');
      }
  }
  
  class Airport extends Model
  {
      //
      public $timestamps = false;
  
      public function schedules()
      {
          return $this-&gt;belongsToMany('App\ScheduleTemplate');
      }
  }
  </code></pre>
  
  <p>When I attempt to query using the following code, I get the error</p>
  
  <blockquote>
    <p>SQLSTATE[42S22]: Column not found: 1054 Unknown column 'vaos_airports.depicao' in 'where clause' (SQL: select * from <code>vaos_airports</code> where <code>vaos_airports</code>.<code>depicao</code> in (1))</p>
  </blockquote>
  
  <pre><code>$schedules = ScheduleTemplate::with('depicao')-&gt;with('arricao')-&gt;get();
  </code></pre>
  
  <p>The end goal is to pull the results into a table. Here is that code if interested.</p>
  
  <pre><code>@foreach($schedules as $s)
       &lt;tr&gt;
            &lt;td&gt;{{$s-&gt;code}}&lt;/td&gt;
            &lt;td&gt;{{$s-&gt;flightnum}}&lt;/td&gt;
            &lt;td&gt;{{$s-&gt;depicao()-&gt;name}}&lt;/td&gt;
            &lt;td&gt;{{$s-&gt;arricao()-&gt;name}}&lt;/td&gt;
            &lt;td&gt;{{$s-&gt;aircraft}}&lt;/td&gt;
            &lt;td&gt;{{$s-&gt;seasonal}}&lt;/td&gt;
            &lt;td&gt;{{$s-&gt;type}}&lt;/td&gt;
       &lt;/tr&gt;
  @endforeach
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>I fixed the relationship problem Apparently I had them swapped. Here are the updated Model classes</p>
  
  <pre><code>class ScheduleTemplate extends Model
  {
      public $table = "schedule_templates";
  
      public function depicao()
      {
          return $this-&gt;belongsTo('App\Models\Airport', 'depicao');
      }
      public function arricao()
      {
          return $this-&gt;belongsTo('App\Models\Airport', 'arricao');
      }
  }
  
  class Airport extends Model
  {
      //
      public $timestamps = false;
  
      public function schedules()
      {
          return $this-&gt;hasMany('App\ScheduleTemplate');
      }
  }
  </code></pre>
  
  <p>The error now lies in the view file. I will either get a BelongsTo error:</p>
  
  <blockquote>
    <p>Undefined property: Illuminate\Database\Eloquent\Relations\BelongsTo::$name</p>
  </blockquote>
  
  <p>or this if I have arricao or depicao without "()"</p>
  
  <blockquote>
    <p>Trying to get property of non-object</p>
  </blockquote>