Lionix Package Docs
  • Lionix Package Docs
  • Laravel Castable Request
  • Laravel Seo Manager
  • Laravel EnvClient
  • Markup Image with React.js
  • Markup Image with Vue.js
  • React Lazy Props
  • Laravel Pagination with ReactJS
  • Laravel Pagination with VueJS
  • Vue Route Middleware
  • Laravel Digitalocean API
Powered by GitBook
On this page
  • Installation
  • Usage
  • Global request casts
  • Todo
  • Credits

Was this helpful?

Laravel Castable Request

PreviousLionix Package DocsNextLaravel Seo Manager

Last updated 4 years ago

Was this helpful?

This package applies eloquent model casts to the request input.

Installation

composer require lionix/castable-request

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 .

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.

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.



namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Lionix\CastableRequest\Contracts\CastsRegistryInterface;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @param \Lionix\CastableRequest\Contracts\CastsRegistryInterface $castRegistry
     *
     * @return void
     */
    public function boot(CastsRegistryInterface $castRegistry)
    {
        $castRegistry->register('created_after', 'date');
    }
}

Todo

  • Add global request casts support for facades and request helper.

Credits

Starting from the Laravel 7.x you can define your own and use it in the request as well.

custom casts
Stas Vartanyan
Lionix Team
eloquent attribute casting
Total Downloads
Version