PHP code example of slashequip / laravel-patchable

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

    

slashequip / laravel-patchable example snippets


use SlashEquip\Patchable\Traits\Patchable;

class Property extends Model
{
    use Patchable;
}

class UpdatePropertyController
{
    public function __invoke(Property $property)
    {
        $property->patch();
    }
}

class Property extends Model
{
    public $patchable = [
        'name',
        'lat',
        'lng',
    ];
}

class Property extends Model
{
    public $patchable = [
        'name' => 'string|max:255',
        'lat' => [
            'numeric',
            'regex:/^[-]?((([0-8]?[0-9])(\.[0-9]+)?)|90(\.0+)?)$/',
        ],
        'lng' => [
            'numeric',
            'regex:/^[-]?((([0-9]|1[0-7][0-9])(\.[0-9]+)?)|180(\.0+)?)$/',
        ],
    ];
}

class Property extends Model
{
    public $patchable = [
        'name' => PropertyNamePatch::class,
    ];
}

use SlashEquip\Patchable\Contracts\Patch;

class PropertyNamePatch implements Patch
{
    public function authorize(Property $property): bool
    {
        return true;
    }

    public function rules(): string|array
    {
        return [
            'string',
            'max:255',
        ];
    }

    public function patch(Property $property, string $key, $value): void
    {
        $property->name = strtolower($value);
    }
}

use SlashEquip\Patchable\Patcher;

class UpdatePropertyController
{
    public function __invoke(Property $property)
    {
        Patcher::patch(
            model: $property,
            patchable: [
                'name' => PropertyNamePatch::class,
                'lat' => [
                    'numeric',
                    'regex:/^[-]?((([0-8]?[0-9])(\.[0-9]+)?)|90(\.0+)?)$/',
                ],
                'lng' => [
                    'numeric',
                    'regex:/^[-]?((([0-9]|1[0-7][0-9])(\.[0-9]+)?)|180(\.0+)?)$/',
                ],
            ],
            attributes: request()->all(),
        )->finally(function (Property $property) {
            $property->save();
        });
    }
}