generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 259
Open
Description
✏️ Describe the bug
I have an eloquent model with a bunch of properties, from which I'm trying to create a laravel data object. I encounter "the constructor requires 14 parameters, 12 given." error, even though the properties are clearly there.
↪️ To Reproduce
Provide us a pest test like this one which shows the problem:
it('cannot validate nested data', function () {
#[MapName(SnakeCaseMapper::class)]
class TextContentData extends Data
{
public string $type = 'text';
public function __construct(
public string|null $text = "",
public string $font,
public int $lineHeight, //this is one of the missing parameters
public int $fontSize, //this is one of the missing parameters
public bool $bold,
public bool $italic,
public Alignments $alignment,
public int|null $characterSpacing,
public string|null $color,
public string|null $background,
public Decorations|null $decoration,
public string|null $decorationColor,
public PdfItemData|Optional $pdfItem,
public DecorationStyles|null $decorationStyle,
) {}
}
class ContentableDataResolver
{
protected static array $map = [
TextContent::class => TextContentData::class
];
public static function resolve(Model $model): Data
{
foreach (self::$map as $class => $dataClass) {
if ($model instanceof $class) {
try {
return $dataClass::from($model);
} catch (\Exception $e) {
throw $e;
dd($model, $e->getMessage());
}
}
}
throw new \Exception("No Data class registered for " . get_class($model));
}
}
class TextContent extends Model
{
protected $fillable = [
'text',
'font',
'fontSize',
'lineHeight',
'bold',
'italic',
'alignment',
'characterSpacing',
'color',
'background',
'decoration',
'decorationColor',
'decorationStyle',
];
}
dd(ContentableDataResolver::resolve(TextContent::find(1))); //though, of course you don't have the data set but you can see what I used in the dump
});Here's the dump of the ContentableDataResolver
Tip\BuilderCore\Models\TextContent {#1425 // vendor/tip/page-builder/src/Data/Resolvers/ContentableDataResolver.php:36
#connection: "mysql"
#table: "text_contents"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:16 [
"id" => 11
"text" => "Write something..."
"font" => "Roboto"
"fontSize" => 12
"lineHeight" => 1
"bold" => 0
"italic" => 0
"alignment" => "center"
"characterSpacing" => null
"color" => "000000"
"background" => null
"decoration" => null
"decorationColor" => null
"decorationStyle" => null
"created_at" => "2025-07-31 07:37:48"
"updated_at" => "2025-07-31 07:37:48"
]
#original: array:16 [
"id" => 11
"text" => "Write something..."
"font" => "Roboto"
"fontSize" => 12
"lineHeight" => 1
"bold" => 0
"italic" => 0
"alignment" => "center"
"characterSpacing" => null
"color" => "000000"
"background" => null
"decoration" => null
"decorationColor" => null
"decorationStyle" => null
"created_at" => "2025-07-31 07:37:48"
"updated_at" => "2025-07-31 07:37:48"
]
#changes: []
#previous: []
#casts: []
#classCastCache: []
#attributeCastCache: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [...]
#touches: []
#relationAutoloadCallback: null
#relationAutoloadContext: null
+timestamps: true
+usesUniqueIds: false
#hidden: []
#visible: []
#fillable: array:13 [
0 => "text"
1 => "font"
2 => "fontSize"
3 => "lineHeight"
4 => "bold"
5 => "italic"
6 => "alignment"
7 => "characterSpacing"
8 => "color"
9 => "background"
10 => "decoration"
11 => "decorationColor"
12 => "decorationStyle"
]
#guarded: array:1 [
0 => "*"
]
}
✅ Expected behavior
For the data class to be initialized properly. I've also tried adding something like this, which also didn't help.
#[MapInputName('line_height')]
🖥️ Versions
Laravel:12
Laravel Data:^4.17
PHP:8.2.14
luigimannoni-smartify
Metadata
Metadata
Assignees
Labels
No labels