'Strange Eloquent belongsTo() return null. Foreign key property being displayed but not accessible
Environment : PHP 8.1.4, Laravel 9
Eloquent Model : Market Model, Sequence Model
Sequence Model belongs to Market Model.
// Sequence.php
public function market()
{
return $this->belongsTo(Market::class);
}
Market and Sequence table has each data properly..
Problem is, Sequence model cannot access its market properly. It looks like $this->market() works fine but $this->market returns null.
// Some method in Sequence.php
private function loadCollection(): CandleCollection
{
dd($this->market(), $this->market);
return $this->collection = $this->market->createCandleCollection($this->label, $this->size);
}
When I dump $this->market(), it returns BelongsTo object and $this->market, it returns null.
Also, $this->market()->toSql() returns
select * from `markets` where `markets`.`id` is null
Strange thing is, it works actually fine just yesterday and I didn't change its code....;; I believe unknown side effect can cause this but I never experience those kine of issue. Any insight help...
Solution 1:[1]
Sorry.. I just found that there is an empty method getAttribute($key).. and coworker said he made some joke.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | GatesPlan |
