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

D3VNZ's avatar
Level 1

Conditional disable GlobalSearch

Has anyone come up with a way to conditionally disable the global search. It doesn't accept a closure so it can't be done with an conditional function.

0 likes
3 replies
LaryAI's avatar
Level 58

To conditionally disable the global search in a Laravel application, you can use middleware to control the behavior based on certain conditions. Here's a step-by-step solution:

  1. Create a Middleware: First, create a middleware that will check the condition and disable the global search if necessary.
php artisan make:middleware DisableGlobalSearch
  1. Implement the Middleware: Open the newly created middleware file (app/Http/Middleware/DisableGlobalSearch.php) and implement the logic to disable the global search based on your condition.
namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class DisableGlobalSearch
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        // Define your condition here
        $shouldDisableSearch = $this->checkCondition();

        if ($shouldDisableSearch) {
            // Disable global search
            config(['app.global_search_enabled' => false]);
        }

        return $next($request);
    }

    /**
     * Check the condition to disable global search.
     *
     * @return bool
     */
    protected function checkCondition()
    {
        // Example condition: disable search if the user is not an admin
        return auth()->check() && !auth()->user()->isAdmin();
    }
}
  1. Register the Middleware: Register the middleware in your app/Http/Kernel.php file. You can add it to the $middleware array if you want it to run for all requests, or to a specific middleware group if you want it to run only for certain routes.
protected $middleware = [
    // Other middleware
    \App\Http\Middleware\DisableGlobalSearch::class,
];
  1. Use the Configuration in Your Application: Finally, use the configuration value to conditionally enable or disable the global search in your application. For example, in your view or controller:
if (config('app.global_search_enabled')) {
    // Render the global search component
} else {
    // Do not render the global search component
}

By following these steps, you can conditionally disable the global search based on any custom logic you define in the middleware.

D3VNZ's avatar
Level 1

This is not correct and does not work

MohamedTammam's avatar

What's your condition? You can call your function to get the boolean value.

return $panel
        // ...
        ->globalSearch(yourFn());

Please or to participate in this conversation.