PHPackages                             lionix/castable-request - 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. lionix/castable-request

ActiveLibrary

lionix/castable-request
=======================

Laravel castable requests

v1.0.1(5y ago)2392.2k↓26.7%MITPHPPHP ^7.2.5|^8.0

Since Sep 7Pushed 2y ago2 watchersCompare

[ Source](https://github.com/lionix-team/castable-request)[ Packagist](https://packagist.org/packages/lionix/castable-request)[ RSS](/packages/lionix-castable-request/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (5)Versions (4)Used By (0)

Laravel Castable Request
========================

[](#laravel-castable-request)

[![Version](https://camo.githubusercontent.com/f7e758de24ad31639aa8a67ff122b803b4fdb252b69c0e50d9dd2a1f21d937e4/68747470733a2f2f706f7365722e707567782e6f72672f6c696f6e69782f6361737461626c652d726571756573742f76657273696f6e)](https://github.com/lionix-team/castable-request/tree/7cd10bf9411f8ae3f9deaad918ae498a49b7db4f/packagist.org/packages/lionix/castable-request/README.md) [![Total Downloads](https://camo.githubusercontent.com/c7d35afed45fa42eeb0db4f7dfa224c34fad4a70e1ca886bd863188c2621b7b7/68747470733a2f2f706f7365722e707567782e6f72672f6c696f6e69782f6361737461626c652d726571756573742f646f776e6c6f616473)](https://github.com/lionix-team/castable-request/tree/7cd10bf9411f8ae3f9deaad918ae498a49b7db4f/packagist.org/packages/lionix/castable-request/README.md)

This package applies eloquent model casts to the request input.

> Laravel SaaS Boilerplate - [Larafast](https://larafast.com)

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

[](#installation)

```
composer require lionix/castable-request

```

Usage
-----

[](#usage)

Implement `Lionix\CastableRequest\Contracts\CastableRequestInterface` in your Request class. You will have to declare `casts` method that will return the attributes that should be casted just like you would do it with [eloquent attribute casting](https://laravel.com/docs/7.x/eloquent-mutators#attribute-casting).

```
namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Lionix\CastableRequest\Contracts\CastableRequestInterface;

class PostsIndexRequest extends FormRequest implements CastableRequestInterface
{
    /**
     * Get request casts.
     *
     * @return array
     */
    public function casts(): array
    {
        return [
            'created_after' => 'date',
        ];
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'created_after' => 'date',
        ];
    }
}
```

The package will do all the magic and when you access the request `created_after` attribute, in this case, it will be casted to an `Illuminate\Support\Carbon` instance.

```
namespace App\Http\Controllers;

use App\Http\Requests\PostsIndexRequest;

class PostsController extends Controller
{
    public function index(PostsIndexRequest $request)
    {
        $createdAfterDiff = $request->input('created_after')->diffForHumans();
        // Example value: 1 month from now
    }
}
```

All default eloquent models castings are available.

Starting from the **Laravel 7.x** you can define your own [custom casts](https://laravel.com/docs/7.x/eloquent-mutators#custom-casts) and use it in the request as well.

### Global request casts

[](#global-request-casts)

If you want to declare casts that will be applied globally without having to define it in each Request class you can use the `Lionix\CastableRequest\Contracts\CastsRegistryInterface` in your service provider and register global casts.

```
