#Lazy loading a relation conditionally returns null and `$this->getAttributes()` always returns empty

16 messages · Page 1 of 1 (latest)

sick steppe
#

Laravel Version

10.10

PHP Version

8.1.7

Database Driver & Version

sqlsrv

Description

I have a model called Stage. and it has a HasMany relation 'products'. Recently the project was upgraded from version 9 to 10.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;

class OrderStage extends \Illuminate\Database\Eloquent\Model
{
    use HasFactory;


    protected $table = 'Order_Stage';

    protected $connection = 'sqlsrv';

    protected $primaryKey = 'OrderID';

    protected $keyType = 'string';

    protected $guarded =[];

    public function products()
    {
        dump($this->getAttributes()); //should not return empty array, but it does.
        return $this->hasMany(OrderProduct::class, 'OrderID', 'OrderID')
            ->where('StageID', $this->StageID);
    }
}

If I do the following:

$stages = OrderStage::take(20)->get()->load('products');
$stages->map->products;

It is supposed to return the products

or

$stages = OrderStage::whereHas('products')->get();

Supposed to return at least 1 OrderStage record.

both returns an empty collection.

Even the dump $this->getAttributes() returns an empty array. I also tried many options such as `$this->getRawOriginal('StageId'). It always returns null

Howver if I do $stages->map->products it returns all the data that I am supposed to get.

#

Steps To Reproduce

To reproduce the error you'll need the following:

  • Create 2 models. OrderStage and Products.
  • Database : MSSQL

OrderStage.php

columns:

image

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;

class OrderStage extends \Illuminate\Database\Eloquent\Model
{
    use HasFactory;


    protected $table = 'Order_Stage';

    protected $connection = 'sqlsrv';

    protected $primaryKey = 'OrderID';

    protected $keyType = 'string';

    protected $guarded =[];

    public function products()
    {
        dump($this->getAttributes()); //should not return empty array, but it does.
        return $this->hasMany(OrderProduct::class, 'OrderID', 'OrderID')
            ->where('StageID', $this->StageID);
    }
}

OrderProduct.php

columns:

image

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;

class OrderProduct extends \Illuminate\Database\Eloquent\Model
{
    use HasFactory;

    protected $table = 'Order_Stage_Products';

    protected $primaryKey = 'Ref';

    public $incrementing = true;
}

Run the following in tinker well or tinker or wherever it seems fit.

$stages = OrderStage::take(20)->get()
->load('products');
dd($stages->map->products);

Expected Output:

image

knotty tangle
#

Hi, did you ever find out a fix to this issue ?

floral fjord
#

Verify inside your AppServiceProvider, if the preventLazyLoading is disabled

#

For the code snippets you provided, seems like the models are configurated for different connections

#

Your stage uses sqlsrv, and products the default

#
  • the relation is not clear
paper ether
#

I don't understand. Why is there an OrderStage model if it's a hasMany relationship?

floral fjord
#

If Stage > Product is a many to many relationship with a OrderStage pivot, it should be a belongsToMany

#

Using the pivot

paper ether
#

OrderProduct doesn't have the same connection as OrderStage.

floral fjord
#

$this->belongsToMany(...)

floral fjord
#

This and the relationships

#

Is not correct if i'm not mistake

copper dune