HaMMerHeD
1 year ago

Lazy loading a relationship works, but eager loading fails

Posted 1 year ago by HaMMerHeD

Good morning, everyone.

So I have these models: BaseReturn and Operator. The BaseReturn class has Operator as a relationship.

(BaseReturn is actually the base of four other models [dead stock, overstock, shipping damage, warranty service], which are different types of returns our store clerks are able to process, and each of which extends BaseReturn.

Operator comes out of a Microsoft SQL Server replicated version our ERP database (Infor SX.e, which uses a Progress OpenEdge database).

BaseReturn comes out of my app database, which is on mysql.

Their connections and tables are configured, the relationships are established, and I am able to pull data from each of them at will. Lovely.

My problem is that lazy loading the operator relationship on the BaseReturn model works as expected, but eager-loading always returns null.

The operator relationship on the BaseReturn class is as follows:

    public function operator()
    {
        return $this->belongsTo(Operator::class, 'operator_id', 'oper2');
    }

(Yes, oper2 is the necessary field in the ERP database, and no, I can't change it.)

It works, in the sense that, if i do something like this:

$return = DeadStockReturn::find(542);
dump($return);
dd($return->operator);

I get a dump of the DeadStockReturn model and the Operator model it's related to.

But if I do this:

$return = DeadStockReturn::find(542);
$return->load('operator');
dump($return);
dd($return->operator);

or this:

$return = DeadStockReturn::with('operator')->find(542);
dump($return);
dd($return->operator);

I see the same DeadStockReturn model from before, but the Operator dump is null.

The problem is, I'm trying to echo this data out as JSON so that I can deal with it on the front end, and of course it gives me null, so none of the properties of the Operator relationship model are available, and I need it for the interface.

Any thoughts? Thanks!

Please sign in or create an account to participate in this conversation.