Skip to content
Advertisement

Error: Object of class AppHttpResourcesProductAttributeResource could not be converted to int

I’m using method in my model to mutate a single response to my client

When call product Laravel using this method name by default?

/**
 * Returns product attributes data to use in the API response.
 *
 * @return IlluminateHttpResourcesJsonAnonymousResourceCollection
 */
public function getAttributes()
{
    return ProductAttributeResource::collection($this->attributes);
}

Debugger show this area:

C:xampphtdocs…/…vendorLaravelframeworksrcIlluminateDatabaseEloquentConcernsHasAttributes.php:521

protected function castAttribute($key, $value)

{
    $castType = $this->getCastType($key);

    if (is_null($value) && in_array($castType, static::$primitiveCastTypes)) {
        return $value;
    }
    switch ($castType) {
        case 'int':
        case 'integer':
            return (int) $value;
        case 'real':

How can I resolve this error?

Advertisement

Answer

in my case i use a reserved method name in my model

for example i define a new attribute() method for my one to many relation that i should choose another name for it

just need to rename your getAttributes to getAttribute method in your model

because this name used by Model Class

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement