Passing Boolean param in laravel via url

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Passing Boolean Parameters in Laravel via URL: A Developer's Guide

Passing data through URLs is a fundamental aspect of web development, and while strings and integers are straightforward, handling boolean values (true or false) introduces a layer of complexity. As noted by the community, how this is managed often depends heavily on the framework you are using. In Laravel, we can certainly achieve this, but it requires understanding how HTTP requests translate into application data.

This guide will walk you through the most practical and robust ways to pass boolean parameters in Laravel via the URL, focusing on clarity, security, and adherence to best practices, referencing concepts found within the broader ecosystem of https://laravelcompany.com.

The Challenge of Booleans in URLs

URLs are fundamentally based on strings. When you use query parameters (e.g., ?is_active=true), the framework receives these as strings. The challenge arises when your backend expects a strict boolean type, not just a string representation. Simply passing true or false often works in simple cases, but robust applications need methods that are predictable and secure.

Method 1: Using Query Strings (The Direct Approach)

The most common way to pass optional flags via the URL is through query strings. This method is simple and effective for small boolean flags.

When a user navigates to a route like /products?show_details=true, Laravel's request object will capture show_details as a string value ("true"). You must then explicitly cast this string back into a boolean within your controller logic.

Example Implementation:

In your route file (routes/web.php):

Route::get('/items', [ItemController::class, 'index']);

In your controller (app/Http/Controllers/ItemController.php):

use Illuminate\Http\Request;

class ItemController extends Controller
{
    public function index(Request $request)
    {
        // Retrieve the query parameter as a string
        $showDetails = $request->query('show_details');

        // Explicitly cast the string value to a boolean
        // Laravel handles 'true'/'false' strings reasonably well, 
        // but explicit casting is safer.
        $isActive = filter_var($showDetails, FILTER_VALIDATE_BOOLEAN);

        if ($isActive) {
            return view('items.show_full');
        } else {
            return view('items.summary');
        }
    }
}

Developer Insight: Using filter_var($value, FILTER_VALIDATE_BOOLEAN) is the recommended PHP approach. It safely converts string representations like "true", "1", "on", etc., into a strict boolean true or false, making your application logic much cleaner and less prone to error, which aligns with strong design principles found in Laravel development.

Method 2: Using Route Parameters (The RESTful Approach)

For more critical flags that define the resource itself rather than just query filtering, route parameters are often superior. Instead of querying a state (?status=true), you define the state directly in the URL structure.

Example Implementation:

In your route file (routes/web.php):

// Route where 'is_active' must be present and is expected to be true or false
Route::get('/products/{is_active}', [ProductController::class, 'show']);

In your controller:

use Illuminate\Http\Request;

class ProductController extends Controller
{
    public function show(Request $request, $is_active)
    {
        // Since route parameters are typically cast as strings, 
        // we must manually convert.
        $isActive = filter_var($is_active, FILTER_VALIDATE_BOOLEAN);

        if (!$isActive) {
            abort(404, "Product not found or inactive.");
        }

        return view('products.details', ['product' => $is_active]);
    }
}

Conclusion: Choosing the Right Tool

While passing booleans via query strings is fast for simple filtering, using route parameters (Method 2) provides a more structured and RESTful approach when the boolean value is essential to the resource being accessed. Always favor explicit type casting within your controllers—using functions like filter_var—to ensure that application logic receives predictable boolean types rather than potentially ambiguous strings. For complex data structures, consider passing the boolean flag alongside a JSON payload in the request body, which allows for much more robust data handling, following standard Laravel practices for API development on https://laravelcompany.com.