php – Receive values in request helper as integer in laravel-ThrowExceptions

Exception or error:

I have a form with a select tag that I am sending data into my laravel blade like below :

 <form action="{{ route('shop.products.index', $product->url_key) }}" method="get">
                        <select style=" font-family: IranSansLight, sans-serif; width:100%" class="mdb-select md-form" name="color_id">
                            <option value="1">1</option>
                            <option value="2">2</option>
                            <option value="3">3</option>
                        </select>
                        <input type="submit" value="send">
                    </form>

and I receive it in the same page as below :

request()->get('color_id')

The problem is this is receiving the data as string but I need the values to be an integer, so how can I get the integer or convert them to integer?

How to solve:
$colorId = (int)request()->get('color_id');

OR

$colorId = intval(request()->get('color_id'))

Leave a Reply

Your email address will not be published. Required fields are marked *