title: Laravel 5 eager loading dropping leading zeros from foreign key
body: <p>I believe that I've encountered a bug in how Laravel 5.3 handles eager loading when the foreign key is a string which contains zerofilled numbers.</p>
  
  <p>I have two models (using a mysql backend), <code>School</code> and <code>Student</code>.  The <code>id</code> field in <code>School</code> is a string, and contains 5-digit numbers assigned by the state, which include leading zeros.  <code>Student</code> <code>BelongsTo</code> <code>School</code>, and contains a <code>school_id</code> field which is defined identically to the <code>id</code> field in <code>School</code>.  I have done some testing, and I'm finding that when I call <code>Student::with('school')</code> I get the expected <code>School</code> models as long as the <code>school_id</code> in <code>Student</code> is free of leading zeroes, but it returns no <code>School</code> models for <code>school_id</code> values with leading zeroes.  </p>
  
  <p>I've done direct testing with freshly minted <code>School</code> records, and the values are being stored correctly with leading zeroes in both database tables, and when I try directly querying the tables the leading zeroes work fine, but as soon as <code>with()</code> enters the equation, things break.  I've attempted to reproduce the failure through other means, even manually constructing <code>whereIn()</code> calls to mirror syntax of the queries constructed by <code>with()</code>, but everything else correctly returns the expected records.</p>
  
  <p>This code worked fine prior to climbing the Laravel upgrade ladder from 4.1 to 5.3, so I'm wondering what may have changed.  I've gone as far as digging into <a href="https://github.com/laravel/framework/blob/5.3/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php" rel="nofollow noreferrer" title="the 5.3 GitHub repository for BelongsTo">the GitHub repository for BelongsTo</a>, and none of the parameter handling seems to strip leading zeroes, so I'm really at a loss as to why <code>with()</code> is breaking in this way.</p>
  
  <p>So, does anybody have any insights they can share?  I'm stumped, and would rather not have to engineer around <code>with()</code>.  I'll also state up front that I can't drop the leading zeroes from the <code>id</code> field, it's not an option, they must actually be stored, and not just displayed as with ZEROFILL.</p>
  
  <p>UPDATE:  I've attached an example that establishes that the <code>school_id</code> stored in <code>Student</code> can successfully connect to the corresponding <code>School</code> when used separately from the <code>with()</code> statement:</p>
  
  <pre><code>    $interventions = Intervention::with('school')-&gt;where('id','=',780)-&gt;get();
      $schools = School::whereIn('id',$interventions-&gt;pluck('school_id')-&gt;all())-&gt;get();
      throw new \Exception(print_r($interventions,true).'|'.print_r($schools,true));
  </code></pre>
  
  <p>Here are the (edited for brevity) results of the \Exception:</p>
  
  <pre><code>Exception in AdminController.php line 273:
  Illuminate\Database\Eloquent\Collection Object
  (
      [items:protected] =&gt; Array
      (
          [0] =&gt; App\Models\Student Object
          (
              [attributes:protected] =&gt; Array
              (
                  [id] =&gt; 780
                  [school_id] =&gt; 01234
              )
              [relations:protected] =&gt; Array
              (
                  [school] =&gt; 
              )
          )
      )
  )
  
  
  Illuminate\Database\Eloquent\Collection Object
  (
      [items:protected] =&gt; Array
      (
          [0] =&gt; App\Models\School Object
          (
              [attributes:protected] =&gt; Array
              (
                  [id] =&gt; 01234
                  [school] =&gt; Test1
                  [district_id] =&gt; 81000
                  [inactive] =&gt; 0
                  [see] =&gt; 0
              )
          )
      )
  )
  </code></pre>
  
  <p>So, while <code>Student::with('school')</code> fails to pull up the corresponding <code>School</code>, feeding the same <code>Student-&gt;school_id</code> values to <code>School::whereIn()</code> succeeds.  I remain mystified.</p>