Posts Learn Components new Snippets new Categories Tags About
/

How to pass boolean in Laravel through URL

Learn how to pass boolean value in Laravel via request URL to easily allow the backend to process the value

Created on Sep 22, 2021

61 views

In Laravel, you can pass a boolean value via the URL to easily allow the backend to process a value such as a toggle or a status. To do so, you can pass either one of the "true" or "false" strings value or 1 for true and 0 for false number.

Laravel Pass in Boolean Value Via URL

http://example.com/docs?download=true // true
http://example.com/docs?download=false // false
Or if you prefer to use the number it will be as follows
http://example.com/docs?download=1 // true
http://example.com/docs?download=0 // false

Laravel Request Boolean


To easily get the boolean value, you can call the "boolean" method of the "Request" class and it will like below.
<?php

Request::boolean('download'); // true or false
If you resolve the Request from the dependency injection then the code will be as follows.
<?php

public function store(Request $request)
{
    if ($request->boolean('downloadable')) {
        // Do something
    }
} 
new

If you like our tutorial, do make sure to support us with some coffee. We need the juice to write the content during the night 🤙🏻