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

mbo's avatar
Level 3

Call to undefined method - laravel impersonate

goodday,

I setting up an package: laravel impersonate.

https://github.com/404labfr/laravel-impersonate

After installing it give the following error if when i use the the following:

what i did:

composer require lab404/laravel-impersonate

Add the service provider at the end of your config/app.php:
'providers' => [
    // ...
    Lab404\Impersonate\ImpersonateServiceProvider::class,
],

Add the trait Lab404\Impersonate\Models\Impersonate to your User model.

placed: Route::impersonate();

run: php artisan vendor:publish --tag=impersonate


By running the following i get the error's

@canBeImpersonated($user)
    <a href="{{ route('impersonate', $user->id) }}">Impersonate this user</a>
@endCanBeImpersonated

error:

Call to undefined method Illuminate\Database\Query\Builder::canBeImpersonated() (View: /Users/mbolivier/Applications/ligplaats/resources/views/my-account/users/index_admin.blade.php)


any help would be appreciated.

thanks

0 likes
5 replies
zoltiecodes's avatar

Hi. Show us where $user comes from. It looks like it's a query builder instance instead of the User model.

zoltiecodes's avatar
Level 14

Whoops. According the docs you have to add that method to the User model. See the README.

  /**
     * @return bool
     */
    public function canBeImpersonated()
    {
        // For example
        return $this->can_be_impersonated == 1;
    }
mbo's avatar
Level 3

zsoltgyure,

Thanks for the reaction. I expected to work with the default Missed some words.

thanks for the sollution!

planetfrank's avatar

You probably already fixed it, but is just that you are missing the trait in your user model like use Impersonate;

Please or to participate in this conversation.