Laravel Eloquent Case Sensitive Relationships
<p>Hi SO I'm having real issues with some Laravel Eloquent relationships which I can only guess are being caused by a case-sensitive relation and I'm hoping somebody here can help!</p>

<p>Here are the models that I'm having the issues with:</p>

<pre><code>class DeliveryManifestLines extends Eloquent
{
    protected $table = 'manifests';

    public function sapDelivery()
    {
        return $this-&gt;hasOne('Delivery', 'DocNum', 'sap_delivery');
    }

}


class Delivery extends Eloquent
{
    protected $connection = 'sap';
    protected $table = 'ODLN';
    protected $primaryKey = 'DocNum';

    public function deliveryManifest() {
      return $this-&gt;belongsTo('DeliveryManifest', 'DocNum', 'sap_delivery');
    }

    public function address()
    {
        return $this-&gt;hasOne('Address', 'Address', 'ShipToCode')-&gt;where('CardCode', $this-&gt;CardCode)-&gt;where('AdresType', 'S');
    }

    public function geolocation()
    {
        return $this-&gt;hasOne('GeoLocation', 'Address', 'ShipToCode')-&gt;where('CardCode', $this-&gt;CardCode)-&gt;where('AdresType', 'S')-&gt;where('Lat', '&gt;', 0)-&gt;where('Lng', '&gt;', 0);
    }
}

class Address extends Eloquent
{
    protected $connection = 'sap';
    protected $table = 'CRD1';
    protected $primaryKey = 'Address';

    public function delivery() {
      return $this-&gt;belongsTo('Delivery', 'Address', 'ShipToCode');
    }

}
</code></pre>

<p>Here's the code in my controller that is supposed to fetch some of the above models from the DB.</p>

<pre><code>$deliveries = DeliveryManifestLines::with('sapDelivery')-&gt;where('manifest_date', $date))-&gt;get();

foreach ($deliveries as $delivery) {
    $delivery-&gt;sapDelivery-&gt;load('address');
}
</code></pre>

<p>I'm using the "<code>-&gt;load('address)</code>" line as no matter what I tried I could not get eager loading to work with "<code>sapDelivery.address</code>"</p>

<p>In 99% of cases the address is loaded successfully from the DB but I have come across one case in which I am experiencing an issue that I can only think is being caused by case-sensitivity.</p>

<p>Using Laravel DebugBar I can see that my application is executing the following query:</p>

<pre><code>SELECT * FROM [CRD1] WHERE [CardCode] = 'P437' AND [AdresType] = 'S' AND [CRD1].[Address] IN ('The Pizza Factory (topping)')
</code></pre>

<p>When I dump the contents of <code>$delivery-&gt;sapDelivery</code> in this occurrence the address relation is NULL, however, when I paste the SQL statement into my DB console and execute it manually I get the expected row returned.</p>

<p>The only difference I can see between this one address and the thousands of others that are working is that there is a case difference between the Address fields:</p>

<p>In the CRD1 table the Address field for the effected/expected row is "The Pizza Factory (Topping)" but the eloquent relationship is using AND [CRD1].[Address] IN ('The Pizza Factory (topping)') to try and find it I'm aware that SQL is case-insensitive be default but I can't think of any other reason why this one row is behaving differently to the others. </p>

<p>Does anybody have any other ideas as to what could be causing this issue and suggest any possible solutions or confirm either way my theory of case sensitivity being the culprit.</p>

<p>Many thanks!</p>