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

getupkid203's avatar

Custom PHP8 attribute such as ObservedBy not working as expected

Howdy all, I've got a Laravel 11 project. My PHP Version is 8.3. Using Homestead on Windows.

I can't get any PHP8 attributes to work as expected. For example:

#[ObservedBy([UserObserver::class])]
class User extends Authenticatable
{
    //
}

My UserObserver::class definitely exists. When I create a UserFactory, the observer is never called.

However, if I register the Observer manually via AppServiceProvider, it works perfectly i.e.:

public function boot(): void
{
    User::observe(UserObserver::class);
}

I was under the impression that as of PHP8.0, these attributes should work. Is there something I may be missing?

0 likes
3 replies
Talinon's avatar

@getupkid203 Make sure you have imported the namespaced UserObserver into your User class.

For example:

use App\Observers\UserObserver;
getupkid203's avatar

Yeah, got it working. Rookie mistake. facepalm

I had imported the Observer class but I didn't import the actual attribute class..........

use Illuminate\Database\Eloquent\Attributes\ObservedBy;

Please or to participate in this conversation.