title: Laravel 8 - Eager loading inconsistent behavior
body: <p>Currently I've these 2 classes</p>
  <p>TransaccionWebPay.php</p>
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  
  namespace App\Models;
  
  use Illuminate\Database\Eloquent\Model;
  
  class TransaccionWebPay extends Model
  {
      protected $table = 'TRANSACCION_WEBPAY';
      protected $primaryKey = 'ID_TRANSACCION_WEBPAY';
      public $incrementing = false;
  
      public static function getByTokenWS($tokenws)
      {
          return TransaccionWebPay::where(&quot;TOKEN_WS&quot;, &quot;=&quot;, $tokenws)-&gt;first();
      }
  
      public function detalle()
      {
          return $this-&gt;hasMany(TransaccionPresupuesto::class,&quot;ID_TRANSACCION_WEBPAY&quot;);
      }
  }
  
  </code></pre>
  <p>TransaccionPresupuesto.php</p>
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  
  namespace App\Models;
  
  use App\Models\Payments\PresupuestoPendiente;
  use Illuminate\Database\Eloquent\Model;
  
  class TransaccionPresupuesto extends Model
  {
      protected $table = 'TRANSACCION_WEBPAY_PRESUPUESTO';
      protected $primaryKey = 'ID_TRANSACCION_WEBPAY_PRESUPUESTO';
      
      public function presupuesto(){
          return $this-&gt;hasOne(PresupuestoPendiente::class,&quot;ID_PRESUPUESTO&quot;,&quot;ID_PRESUPUESTO&quot;);
      }
      
  }
  </code></pre>
  <p>it happens that, when I try to eager load TransaccionWebPay using</p>
  <pre class="lang-php prettyprint-override"><code>$response = TransaccionWebPay::with(['detalle'])
                  -&gt;where(&quot;TOKEN_WS&quot;, &quot;=&quot;, $id)
                  -&gt;first();
  </code></pre>
  <p>detalle has not records (which is wrong)</p>
  <p>however, if I do</p>
  <pre class="lang-php prettyprint-override"><code>$response = TransaccionWebPay-&gt;where(&quot;TOKEN_WS&quot;, &quot;=&quot;, $id)-&gt;first();
  $detalle = $response-&gt;detalle // there are records! (and this is the expected behavior)
  </code></pre>
  <p>Note: it works for some records and it does not for others. which is very strange.
  when I do debug the query, I notice that this happens when &quot;with&quot; does not work</p>
  <pre><code>array:2 [
    0 =&gt; array:3 [
      &quot;query&quot; =&gt; &quot;select * from `TRANSACCION_WEBPAY` where `TOKEN_WS` = ? limit 1&quot;
      &quot;bindings&quot; =&gt; array:1 [
        0 =&gt; &quot;1131f9a53xxx&quot;
      ]
      &quot;time&quot; =&gt; 1.52
    ]
    1 =&gt; array:3 [
      &quot;query&quot; =&gt; &quot;select * from `TRANSACCION_WEBPAY_PRESUPUESTO` where `TRANSACCION_WEBPAY_PRESUPUESTO`.`ID_TRANSACCION_WEBPAY` in (0)&quot;
      &quot;bindings&quot; =&gt; []
      &quot;time&quot; =&gt; 1.07
    ]
  ]
  </code></pre>
  <p>regarding to the second query... I'm not sure why eloquent is querying  &quot;TRANSACCION_WEBPAY_PRESUPUESTO<code>.</code>ID_TRANSACCION_WEBPAY` in (0)&quot;. did I missed anything?</p>
  <p>I'm struggling to find why if I'm making some mistake to define the relationships or if missed something.</p>
  <p>Thanks!</p>