Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

socieboy's avatar

Error when query on User model

I'm getting this error when i do a query on the User model, the column notification emails is cast to array, so i don't really know what that means, any idea?

Call to undefined cast [Array] on column [notification_emails] in model [App\Models\User].
0 likes
11 replies
automica's avatar

How are your casting? What does your $casts array look like?

Can you show your code?

rodrigo.pedra's avatar

Primitive cast names are case-sensitive. Use a lowercase array for your cast.

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable implements MustVerifyEmail
{
    use HasFactory, Notifiable;
    
    // other code

    protected $casts = [
        // ... other casts
        'notification_emails' => 'array', // all lower case
    ];
}

As Laravel can't figure which cast type you want, it tries instantiating a custom cast class named Array.

socieboy's avatar

I’m using array in lowercase. This error appears after upgrading to Laravel 8

1 like
rodrigo.pedra's avatar
Level 56

The error message is from Illuminate\Database\Eloquent\InvalidCastException

https://github.com/laravel/framework/blob/325a335ccf45426eabb27131ed48aa6114434c99/src/Illuminate/Database/Eloquent/InvalidCastException.php#L42

And this exception is only thrown from the Illuminate\Database\Eloquent\Concerns\HasAttributes trait used by the base Eloquent Model class

https://github.com/laravel/framework/blob/325a335ccf45426eabb27131ed48aa6114434c99/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php#L1178-L1195

When it is checking for a cast it couldn't find.

You either have the cast set with a capital A somewhere (check also for ->withCasts(...) usage in a query result), or have a cache problem (remove any .php files from your ./bootstrap/cache folder and then run composer dump-autoload).

Hope some of it helps.

1 like
mengthong's avatar

Here My error message

Call to undefined cast [numeric] on column [from] in model

rodrigo.pedra's avatar

@mengthong there isn't a cast called numeric.

You can see the list of the built-in casts in the docs:

https://laravel.com/docs/10.x/eloquent-mutators#attribute-casting

You might try either integer, decimal, double, float, or real. (note that the decimal cast requires a precision parameter)

If you need a cast that works with both integers and floats, you can try writing a custom cast:

https://laravel.com/docs/10.x/eloquent-mutators#custom-casts

Note: please do not reply to threads so old. This thread is 2 years old already. Laravel changes in a fast pace, so things can be quite different, and some responders are not active after a while.

You are better off creating a new thread. Linking to an old thread as an example of what you have already attempted is fine.

2 likes

Please or to participate in this conversation.