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.