定制 lionix/castable-request 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

lionix/castable-request

Composer 安装命令:

composer require lionix/castable-request

包简介

Laravel castable requests

README 文档

README

Version Total Downloads

This package applies eloquent model casts to the request input.

Laravel SaaS Boilerplate - Larafast

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 eloquent 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 and use it in the request as well.

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.

<?php

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

统计信息

  • 总下载量: 93.16k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 23
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 23
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-09-07

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固