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

jdbih's avatar
Level 1

Call to undefined method Illuminate\Database\Eloquent\Casts\Attribute::make() On Laravel 9

After trying to create an Accessor in Larvel 9 i get this error:Call to undefined method Illuminate\Database\Eloquent\Casts\Attribute::make().

My code:

<?php

namespace Modules\Protocol\Entities;

use Illuminate\Database\Eloquent\Casts\Attribute;
use DateTime;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Protocol extends Model
{
    use HasFactory;

    protected $fillable = ['document_type', 'description_id', 'subject', 'employee_name', 'date_recived_sent', 'sender_id', 'sender_document_ref', 'subId', 'userId', 'department_id'];
    
    protected $with = ['contact', 'department'];

    public function contact() {
        return $this->hasOne('Modules\Contact\Entities\Contact', 'id', 'sender_id');
    }

    public function department() {
        return $this->belongsTo('Modules\Department\Entities\Department', 'department_id', 'id');
    }
    /**
     * 
     *
     * @return \Illuminate\Database\Eloquent\Casts\Attribute
     */
    protected function PrettySRDate(): Attribute {
        return Attribute::make(
            get: fn () => (new DateTime($this->date_recived_sent))->format('d.m.Y')
        );
    }
    
    protected static function newFactory()
    {
        return \Modules\Protocol\Database\factories\ProtocolFactory::new();
    }
}
0 likes
7 replies
nabeel.s's avatar

Looks like the docs are out of date, maybe. I'm just doing this now:

return new Attribute(get: fn() => ..., set: fn() => ...);
8 likes
drewgallagher's avatar

Unfortunately this still hasn't been fixed. Is there any way to get a hold of the admins to fix this? Had a production bug and I couldn't resolve it because of this very issue. Also the one example they have of new Attribute is not helpful and doesn't tell me the structure of the get and set functions.

HMLTech's avatar

I'm a little late, but this is the first result of Google for this error so hopefully it helps someone.

You need to make sure to import

use Illuminate\Database\Eloquent\Casts\Attribute;

rather than

use Attribute;
1 like
Hadayat's avatar

In my case I was working on old Laravel 8 project and I am using Laravel 10 syntax, then I switched back to old one and it worked like a charm.

Please or to participate in this conversation.