Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data Objects having inconsitencies. #929

Closed
1e4 opened this issue Jan 13, 2025 · 0 comments
Closed

Data Objects having inconsitencies. #929

1e4 opened this issue Jan 13, 2025 · 0 comments

Comments

@1e4
Copy link

1e4 commented Jan 13, 2025

✏️ Describe the bug
I've been stuck on this the past few hours and unable to make any headway into solving it. I'm getting an error with a single model and getting the following

Could not create App\\Data\\UserHolidayData: the constructor requires 3 parameters, 1 given. Parameters given: id. Parameters missing: holStart, holEnd

The code that doesn't work

$holiday = UserHoliday::find(1512);
$data = UserHolidayData::from($holiday);

dump($holiday, $data);

Image

The code that does work for another model

$stat = UserInfo::find(1512);
dump($stat, UserInfoData::from($stat));

Image

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class UserHoliday extends Model
{
    protected $table = 'user_holiday';
    public $incrementing = false;
    public $timestamps = false;

    protected $casts = [
        'holStart' => 'int',
        'holEnd'   => 'int',
    ];

    protected $fillable = [
        'holStart',
        'holEnd',
    ];
}
<?php

namespace App\Data;

use Spatie\LaravelData\Data;

class UserHolidayData extends Data
{

    public function __construct(
        public int $id,
        public int $holStart,
        public int $holEnd,
    )
    {
    }
}

I can however get this to work by adding toArray()

$holiday = UserHoliday::find(1512);
$data = UserHolidayData::from($holiday->toArray());

I was planning on using nested Data Objects with constructor promotion so I can't call ->toArray().

I am utterly confused and stumped, I've debugged as far as I can, going so far as to looking into how DataFromArrayResolver works

The createData() in DataFromArrayResolver only return an $id property which explains the null values, but why is that only returning an ID? So back we go to ResolvedDataPipeline L42

    if (! is_array($properties)) {
        $properties = $this->transformNormalizedToArray($properties);
    }

If I dump($properties) before and after this, it returns only an ID after

That's pretty much as far as I got and still don't have an answer as to why this one particular model requires ->toArray()

I've tried to include all the information possible, if you need more please let me know.

✅ Expected behavior

I expect to not need to call ->toArray() when it should handle this model automatically.

🖥️ Versions

Laravel: 11.37.0
Laravel Data: 4.11.1
PHP:8.4

@spatie spatie locked and limited conversation to collaborators Jan 17, 2025
@rubenvanassche rubenvanassche converted this issue into discussion #933 Jan 17, 2025

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant