PHP code example of georgehanson / laravel-persisters

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

    

georgehanson / laravel-persisters example snippets



use GeorgeHanson\LaravelPersisters\BasePersister;

class MyPersister extends BasePersister
{
    /**
     * Create a new Model
     *
     * @param array $data
     * @return Model
     */
    protected function create(array $data)
    {
        // Store a new resource here
    }

    /**
     * Update the given Model
     *
     * @param array $data
     * @param Model $model
     * @return Model
     */
    protected function update(array $data, Model $model)
    {
        // Update the given model here
    }
}


$data = [
    'first_name' => 'John',
    'last_name' => 'Doe'
];

$persister = new MyPersister();

$persister->persist($data);



use Illuminate\Http\Request;
use App\Persisters\MyPersister;

class MyController extends Controller
{
    public function store(Request $request, MyPersister $persister) 
    {
        $record = $persister->persist($request);
    }
}



use Illuminate\Http\Request;
use App\Persisters\MyPersister;
use App\User;

class MyController extends Controller
{
    public function update($id, Request $request, MyPersister $persister) 
    {
        $user = User::find($id);
        
        // Update the user with the given data
        $record = $persister->persist($request, $user);
    }
}



use GeorgeHanson\LaravelPersisters\BasePersister;

class MyPersister extends BasePersister
{
    /**
     * The data to filter
     * 
     * @type array
     */
    public $keys = [
        "first_name",
        "last_name"
    ];

    /**
     * Create a new Model
     *
     * @param array $data
     * @return Model
     */
    protected function create(array $data)
    {
        // No matter what is passed to us, $data will only contain "first_name" and "last_name"
    }

    /**
     * Update the given Model
     *
     * @param array $data
     * @param Model $model
     * @return Model
     */
    protected function update(array $data, Model $model)
    {
        // Update the given model here
    }
}