reza_dentalserve_net's avatar

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

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

0 likes
1 reply

Please or to participate in this conversation.