PHP code example of oguzhankrcb / auto-casting-json-resource

1. Go to this page and download the library: Download oguzhankrcb/auto-casting-json-resource 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/ */

    

oguzhankrcb / auto-casting-json-resource example snippets


use AutoCastingJsonResource;

    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        return $this->autoCast(parent::toArray($request));
    }

    public function casts(): array
    {
        return [
            'integer' => fn ($value) => (int) ($value / 100), // It will divide all integer objects with 100
            Money::class => fn (Money $value) => $value->getMinorAmount()->toInt() / 2, // It will cast all Brick\Money\Money objects to integer and divide them with 2
        ];
    }

    public function excludedColumns(): array
    {
        return [
            'id', // id column will be excluded from castings
        ];
    }