PHP code example of novius / laravel-json-casted

1. Go to this page and download the library: Download novius/laravel-json-casted 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/ */

    

novius / laravel-json-casted example snippets


namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Novius\LaravelJsonCasted\Services\JsonCasted;

class Post extends Model {

    protected $casts = [
        'extras' => JsonCasted::class.':getExtrasCasts',
    ];
    
    public function getExtrasCasts(): array
    {
        return [
            'date' => 'date:Y-m-d',
        ];
    }
}

namespace App\Casts;

use Novius\LaravelJsonCasted\Services\JsonCasted;

class Extras extends JsonCasted {

    protected $casts = [
        'date' => 'date:Y-m-d',
    ];
}

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Casts\Extras;

class Post extends Model {

    protected $casts = [
        'extras' => Extras::class,
    ];
}


    $model = Post::first();
    // $model->extras->date is a now Carbon class 
    $model->extras->date->lt(now());