Home / Snippets / How to pass boolean in Laravel through URL
How to pass boolean in Laravel through URL cover

How to pass boolean in Laravel through URL

1.6K

2 years ago

0 comments

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
    }
} 
notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

If you like our tutorial, support us by being our Patreon or buy us some coffee ☕️

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this