PHP code example of morrislaptop / laravel-popo-caster

1. Go to this page and download the library: Download morrislaptop/laravel-popo-caster 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/ */

    

morrislaptop / laravel-popo-caster example snippets


namespace App\Models;

use App\Values\Address;
use Illuminate\Database\Eloquent\Model;
use Morrislaptop\LaravelPopoCaster\Serializer;

/**
 * @property Address $address
 */
class User extends Model
{
    protected $casts = [
        'address' => Serializer::class . ':' . Address::class,
        'prev_addresses' => Serializer::class . ':' . Address::class . '[]',
    ];
}

$user = User::create([
    // ...
    'address' => [
        'street' => '1640 Riverside Drive',
        'suburb' => 'Hill Valley',
        'state' => 'California',
        'moved_at' => now(),
    ],
    'addresses' => [
        [
            'street' => '42 Wallaby Way',
            'suburb' => 'Sydney',
            'state' => 'NSW',
            'moved_at' => '2020-01-14T00:00:00Z',
        ],
    ]
])

$residents = User::where('address->suburb', 'Hill Valley')->get();

$user->address->toMapUrl();

$user->address->getCoordinates();

$user->address->getPostageCost($sender);

$user->address->calculateDistance($otherUser->address);

$user->address->moved_at->diffForHumans();

echo (string) $user->address;

namespace App\Models;

use App\Values\Money;
use Illuminate\Database\Eloquent\Model;
use Morrislaptop\LaravelPopoCaster\Normalizer;

/**
 * @property Money $money
 */
class User extends Model
{
    protected $casts = [
        'money' => Normalizer::class . ':' . Money::class,
    ];
}

$user = User::create([
    // ...
    'amount' => 1000,
    'curency' => 'AUD',
]);

$user = User::create([
    // ...
    'money' => new Money(1000, 'AUD'),
])

$user->money->convertTo('USD');