PHPackages                             vollborn/laravel-request-cast - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. vollborn/laravel-request-cast

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

vollborn/laravel-request-cast
=============================

Allows to cast request values into their supposed types

v1.0.0(3y ago)24MITPHPPHP ^8.0

Since Aug 21Pushed 3y ago1 watchersCompare

[ Source](https://github.com/vollborn/laravel-request-cast)[ Packagist](https://packagist.org/packages/vollborn/laravel-request-cast)[ RSS](/packages/vollborn-laravel-request-cast/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

Laravel Request Cast
====================

[](#laravel-request-cast)

Laravel Request Cast is a small package to cast Laravels request values into their supposed types.

For example: If you build an API and validate a request parameter as int, the value attached to the request object can still be a string. It only validates the content, but does not change its data type.

This JSON body would resolute as a string in your controller, because it is a string in the request itself too:

```
{
    "integer": "1251"
}
```

This package aims to fix this inconvenience. The same request will resolute as an int in your controller, if used correctly.

Installation
------------

[](#installation)

This package is available at composer. To install it, run:

```
composer require vollborn/laravel-request-cast

```

Usage
-----

[](#usage)

There are two ways of using this package.
You can either...

1. Use it as an extended class
2. or as a trait if you need to extend something else.

Both will work the same way, they are just different writing styles.

### Using a parent class

[](#using-a-parent-class)

This is Laravel's default request.

```
use Illuminate\Foundation\Http\FormRequest;

class TestRequest extends FormRequest
{
    ...
```

Once we change the extended class, we should be up and running!

```
use Vollborn\LaravelRequestCast\Classes\CastedRequest;

class TestRequest extends CastedRequest
{
    ...
```

### Using a trait

[](#using-a-trait)

Simply add the "Casts" trait to your request.
That should be it.

```
use Illuminate\Foundation\Http\FormRequest;
use Vollborn\LaravelRequestCast\Traits\Casts;

class TestRequest extends FormRequest
{
    use Casts;
    ...
```

### Casting values

[](#casting-values)

Casting values is pretty easy. You just need to add the "casts" function to your request, just like the "rules" function:

```
