0

Laravel Framework 8.61.0

Model/Order.php

<?php

class Order extends Model
{
    public client(): HasOne
    {
        return $this->hasOne(Client::class, 'customer_fk', 'customer_key');
    }
}

This is a simple example for me to build the relationship with order and client, I can get client name without using eager loading, which means the relationship should be correct, but I don't want to have N+1 problems so I need eager loading, it's working perfectly on the same version of PHP and Laravel on MySQL, how to fix it on mssql?

<?php

$order = Order::find(1);

echo $order->client->name; // return correct name

$order = Order::with(['client'])->find(1);

echo $order->client->name; // ErrorException : Trying to get property 'name' of non-object
Chan
  • 1,947
  • 6
  • 25
  • 37
  • Are you sure that worked with a different DB? Bcs AFAIK that is not how to do with/find, eg a quick search shows: https://stackoverflow.com/questions/16864010/laravel-eloquent-with-and-find – Don't Panic Oct 06 '21 at 08:48
  • Does this answer your question? [Laravel Eloquent with and find](https://stackoverflow.com/questions/16864010/laravel-eloquent-with-and-find) – Don't Panic Oct 06 '21 at 08:48
  • https://i.imgur.com/fLUHvqy.png it's work on MySQL – Chan Oct 07 '21 at 01:10

0 Answers0