Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/vollborn/laravel-request-cast

Allows to cast request values into their supposed types.
https://github.com/vollborn/laravel-request-cast

casting controller laravel laravel-framework mvc requests validation

Last synced: 18 days ago
JSON representation

Allows to cast request values into their supposed types.

Awesome Lists containing this project

README

        

# 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:
```json
{
"integer": "1251"
}
```

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

## Installation

This package is available at composer.
To install it, run:
```
composer require vollborn/laravel-request-cast
```

## 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

This is Laravel's default request.
```php
use Illuminate\Foundation\Http\FormRequest;

class TestRequest extends FormRequest
{
...
```

Once we change the extended class, we should be up and running!
```php
use Vollborn\LaravelRequestCast\Classes\CastedRequest;

class TestRequest extends CastedRequest
{
...
```

### Using a trait

Simply add the "Casts" trait to your request.

That should be it.

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

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

### Casting values

Casting values is pretty easy. You just need to add the "casts" function to your request, just like the "rules" function:
```php
CastTypes::BOOLEAN
];
}

...
}
```
This request would cast the "test" parameter to a boolean.

Following cast types are available:
```php
# Strings
CastTypes::STRING

# Booleans
CastTypes::BOOLEAN
CastTypes::BOOL

# Integers
CastTypes::INTEGER
CastTypes::INT

# Arrays
CastTypes::ARRAY
```

**Please note:**

Casted values can be null. For example: If you cast to a boolean, the value can be *true*, *false* OR *null*.

This is supposed to be like that. If the value is null, your validation / rules will check if it is valid or not.

## Examples

Here are some example requests using casts. Feel free to copy them and try it out!

### Using a parent class

```php
CastTypes::BOOLEAN
];
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
{
return [
'test' => 'nullable|boolean'
];
}
}
```

### Using a trait

```php
CastTypes::BOOLEAN
];
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
{
return [
'test' => 'nullable|boolean'
];
}
}
```