KamaZz
2 months ago
274
4
Laravel

Relationship returns empty array

Posted 2 months ago by KamaZz

Hi, I have a models:

    public function phases()
    {
        return $this->hasMany('App\Models\Phase');
    }

    public function buildings()
    {
        return $this->phases->map(fn($phase) => $phase->buildings)->flatten();
    }

    public function albums()
    {
        return $this->buildings()->map(fn($building) => $building->albums)->flatten();
    }

    public function changes()
    {
        return $this->albums()->map(fn($album) => $album->changes)->flatten();
    }

$project->albums() works well, $project->buildings() works well. Everything works well except for $project->changes() it returns an empty array. Without flatten it returns empty arrays equal to the number in the Albums model. In the logs, I do not see any queries against the "changes" table.

What am I doing wrong?

Please sign in or create an account to participate in this conversation.