Show Laravel Debugbar Only to Specific Users
Hello artisans! In this article, I’m going to share how to show Laravel Debugbar only to specific users. Let’s see:
Table of Contents
Create a Middleware
Run this command to create a middleware named DebugBarMiddleware:
php artisan make:middleware DebugBarMiddleware
Then register the middleware to Kernel.php in $middleware
array:
// register globally
protected $middleware = [
//
\App\Http\Middleware\DebugBarMiddleware::class,
];
// or register in a speceific route group
protected $middlewareGroups = [
'web' => [
//
\App\Http\Middleware\DebugBarMiddleware::class,
],
];
Show via User ID
Open the DebugBarMiddleware from app\Http\Middleware
location and set user ids like:
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
if (auth()->user() && in_array(auth()->id(), [1,5,10])) {
\Debugbar::enable();
} else {
\Debugbar::disable();
}
return $next($request);
}
Show via User IP
We can also specify user IPs. Open the middleware and set user IPs to $allowd_ips
array.
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
$allowd_ips = ['192.168.2.3', '192.168.4.50', '192.168.21.35'];
if(in_array(request()->ip(), $allowd_ips)) {
\Debugbar::enable();
} else {
\Debugbar::disable();
}
return $next($request);
}
That’s all. Thanks for reading. ?
Comment
Preview may take a few seconds to load.
Markdown Basics
Below you will find some common used markdown syntax. For a deeper dive in Markdown check out this Cheat Sheet
Bold & Italic
Italics *asterisks*
Bold **double asterisks**
Code
Inline Code
`backtick`Code Block```
Three back ticks and then enter your code blocks here.
```
Headers
# This is a Heading 1
## This is a Heading 2
### This is a Heading 3
Quotes
> type a greater than sign and start typing your quote.
Links
You can add links by adding text inside of [] and the link inside of (), like so:
Lists
To add a numbered list you can simply start with a number and a ., like so:
1. The first item in my list
For an unordered list, you can add a dash -, like so:
- The start of my list
Images
You can add images by selecting the image icon, which will upload and add an image to the editor, or you can manually add the image by adding an exclamation !, followed by the alt text inside of [], and the image URL inside of (), like so:
Dividers
To add a divider you can add three dashes or three asterisks:
--- or ***

Comments (0)