PHP code example of asdh / save-model

1. Go to this page and download the library: Download asdh/save-model 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/ */

    

asdh / save-model example snippets


// config/save_model.php

return [
    /**
     * The directory name where the files should be stored
     * This can be changed via 'saveableFields' method on model
     */
    'file_upload_directory' => 'files',
];

// In controller

use Asdh\SaveModel\SaveModel;

SaveModel::new(
    new User,
    $request->only(['name', 'email', 'password', 'image'])
)->execute();

// OR

(new SaveModel(
    new User,
    $request->only(['name', 'email', 'password', 'image'])
)->execute();

// In controller

use Asdh\SaveModel\SaveModel;

$user = User::find(1);

SaveModel::new(
    $user,
    $request->only(['name', 'email'])
)->execute();

use Asdh\SaveModel\Contracts\CanBeSavedContract;

class User extends Authenticatable implements CanBeSavedContract
{

}

use Asdh\SaveModel\Contracts\CanBeSavedContract;
use Asdh\SaveModel\Fields\DatetimeField;
use Asdh\SaveModel\Fields\FileField;
use Asdh\SaveModel\Fields\PasswordField;
use Asdh\SaveModel\Fields\StringField;

class User extends Authenticatable implements CanBeSavedContract
{
    public function saveableFields(): array
    {
        return [
            'name' => StringField::new(),
            'email' => StringField::new(),
            'email_verified_at' => DatetimeField::new(),
            'password' => PasswordField::new(),
            'image' => FileField::new(),
        ];
    }
}

use Asdh\SaveModel\SaveModel;

SaveModel::new(
    new User,
    $request->only(['name', 'email', 'password', 'image'])
)->execute();

// OR

(new SaveModel(
    new User,
    $request->only(['name', 'email', 'password', 'image'])
)->execute();

// app/Models/User.php

public function saveableFields(): array
{
    return [
        'image' => FileField::new()->setDirectory('images'),
    ];
}

// app/Models/User.php

public function saveableFields(): array
{
    return [
        'image' => FileField::new()
            ->setDirectory('images')
            ->setDisk('s3'),
    ];
}

// app/Models/User.php

use Illuminate\Http\UploadedFile;

public function saveableFields(): array
{
    return [
        'image' => FileField::new()
            ->setDirectory('images')
            ->setFileName(function (UploadedFile $uploadedFile) {
                return $uploadedFile->getClientOriginalName();
            }),
    ];
}

// app/Models/User.php

use Illuminate\Http\UploadedFile;

public function saveableFields(): array
{
    return [
        'image' => FileField::new()
            ->setDirectory('images')
            ->uploadAsOriginalName(),
    ];
}

// app/Models/User.php

use Illuminate\Http\UploadedFile;

public function saveableFields(): array
{
    return [
        'image' => FileField::new()
            ->setDirectory('images')
            ->dontDeleteOldFileOnUpdate(),
    ];
}

Asdh\SaveModel\Fields\StringField::class
Asdh\SaveModel\Fields\IntegerField::class
Asdh\SaveModel\Fields\DatetimeField::class
Asdh\SaveModel\Fields\DateField::class
Asdh\SaveModel\Fields\TimeField::class
Asdh\SaveModel\Fields\PasswordField::class
Asdh\SaveModel\Fields\FileField::class
Asdh\SaveModel\Fields\BooleanField::class



namespace App\ModelFields;

use Asdh\SaveModel\Fields\Field;

class BooleanField extends Field
{
    public function execute(): mixed
    {
        // Perform your logic and return the value...

        // return strtoupper($this->value)
    }
}



namespace App\ModelFields;

use Asdh\SaveModel\Fields\Field;

class BooleanField extends Field
{
    public function execute(): mixed
    {
        return in_array($this->value, [1, '1', true, 'true', 'on', 'yes']);
    }
}


use Asdh\SaveModel\Contracts\CanBeSavedContract;
use Asdh\SaveModel\Fields\DatetimeField;
use Asdh\SaveModel\Fields\FileField;
use Asdh\SaveModel\Fields\PasswordField;
use Asdh\SaveModel\Fields\StringField;
use App\ModelFields\BooleanField;

class User extends Authenticatable implements CanBeSavedContract
{
    public function saveableFields(): array
    {
        return [
            'name' => StringField::new(),
            'email' => StringField::new(),
            'email_verified_at' => DatetimeField::new(),
            'password' => PasswordField::new(),
            'image' => FileField::new(),
            'is_admin' => Boolean::new(),
        ];
    }
}

bash
php artisan save-model:publish

or

php artisan vendor:publish --provider="Asdh\SaveModel\SaveModelServiceProvider"