In Laravel Nova 4, you can use the Nova::isImpersonated() method to determine if impersonation is currently active. You can create a custom Blade directive to achieve the desired functionality.
First, open your AppServiceProvider and add the following code to the boot method:
use Laravel\Nova\Nova;
public function boot()
{
Nova::userImpersonated(function () {
Blade::directive('notImpersonating', function () {
return "<?php if (!\Laravel\Nova\Nova::isImpersonated()) { ?>";
});
Blade::directive('endnotImpersonating', function () {
return "<?php } ?>";
});
});
}
This code registers two Blade directives: @notImpersonating and @endnotImpersonating. These directives will only be active when impersonation is not active.
Now, you can use these directives in your views like this:
@notImpersonating
<!-- Only include this if we are not impersonating a user -->
<x-analytics />
@endnotImpersonating
The x-analytics component will only be included if impersonation is not active.
Remember to run php artisan optimize or php artisan serve to apply the changes.
Note: This solution assumes you are using Laravel Nova 4. If you are using a different version, please refer to the official Laravel Nova documentation for the appropriate method.