laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.5k stars 11.01k forks source link

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

Closed rezadsn closed 1 year ago

rezadsn commented 1 year ago

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:

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

driesvints commented 1 year ago

Hey there,

Can you first please try one of the support channels below? If you can actually identify this as a bug, feel free to open up a new issue with a link to the original one and we'll gladly help you out.

Thanks!