PHP code example of atayahmet / laravel-castable

1. Go to this page and download the library: Download atayahmet/laravel-castable library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

atayahmet / laravel-castable example snippets


Castable\CastableServiceProvider::class




namespace App\Http\Requests;

use Castable\Castable;

class ContactRequest extends Castable
{
    protected $casts = [
        'json' => [
            //
        ],
        'post'  => [
            'name' => 'string',
            'age' => 'integer',
            'student' => 'boolean',
            'interests' => 'collection'
        ],
        'query' => [
            'save' => 'boolean'
        ]
    ];

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return false;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            //
        ];
    }
}



ContactController extends Controller {

  public index(ContactRequest $contactRequest)
  {
      $contactRequest->cast()->input();
  }
}

$contactRequest->cast()->input('interests'); // collection

$contactRequest->cast()->input('student') // boolean (true)

$contactRequest->cast()->input('save') // boolean (true)

$contactRequest->cast()->json();

$contactRequest->cast()->json('age');

$contactRequest->input();

$contactRequest->input('student'); // string (true)

$contactRequest->json();


public function PostNameAttribute($value, $original)
{
    return ucfirst($value);
}


public function QuerySaveAttribute($value, $original)
{
    return $value === true ? 1 : 0;
}


public function JsonSaveAttribute($value, $original)
{
    return ucfirst($name);
}
sh
$ php artisan make:cast ContactRequest