Skip to content

nickescobedo/Cambia

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

About

Cast request input in Laravel.

Installation

composer require nickescobedo/cambia

Basic Usage

  1. Add NickEscobedo\Cambia\CastRequestAttributes trait to your request class.
  2. Add a casts function to your request class that returns an array.
  3. Access casts via request attributes $request->castedInput('inputKey'). Note: The request key must have be validated in rules before it will attempt to cast.

Example

class Cast extends FormRequest
{
    use CastRequestAttributes;
    
    public function rules(): array
    {
        return [
            'toBoolean' => 'string', // Fields not present in validation will not cast
        ];
    }

    public function casts(): array
    {
        return [
            'toBoolean' => 'boolean',
        ];
    }
}

$request->castedInput('toBoolean') will return a boolean value.

Available Casts

  • int
  • integer
  • float
  • double
  • decimal:int
  • string
  • boolean
  • object
  • array
  • json
  • collection
  • date
  • datetime or datetime:format
  • immutable_date
  • immutable_datetime or immutable_datetime:format
  • timestamp

Custom Casts

Custom casts allow for more complex casting logic. Enums and custom classes can be used. Custom cast classes must implement NickEscobedo\Cambia\CastsRequestAttributes.

Enum

enum Status: string
{
    case Pending = 'pending';
}
public function casts(): array
{
    return [
        'status' => Status::class,
    ];
}

Custom Cast Class

The class must implement NickEscobedo\Cambia\CastsRequestAttributes.

use Illuminate\Http\Request;
use NickEscobedo\Cambia\CastsRequestAttributes;

class JsonCast implements CastsRequestAttributes
{

    public function get(Request $request, string $key, mixed $value, array $attributes)
    {
        return json_decode($value, true);
    }
}
class Cast extends FormRequest
{
    use CastRequestAttributes;

    public function rules(): array
    {
        return [
            'toBoolean' => 'string', // Fields not present in validation will not cast
        ];
    }

    public function casts(): array
    {
        return [
            'toBoolean' => JsonCast::class,
        ];
    }
}