PHP code example of laimoon / model-updater

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

    

laimoon / model-updater example snippets


class UserController extends Controller
{
    public function update(Request $request)
    {
        $user = auth()->user();

        // now you check for every field you allowed to update and perform the needed logic.
        if($request->has('name')) {
            // needed logic and validation
            ...
            ..
            $user->name = $request->name
        }

        if($request->has('email')) {
            // needed logic and validation
            ...
            ..
            $user->email = $request->email
        }

        // update/save the model
        $user->save();

        // return a response
        return response()->json();
    }
}

//App/Http/Controllers
use App\Updaters\UserUpdater;

class UserController extends Controller
{
    public function store(UserUpdater $updates)
    {
        auth()->user()->fillUpdates($updates);
    }
}

// App/Updaters
class UserUpdater extends Updater
{
    /**
     * Allowed fields to be updated.
     */
    protected $fields = ['name', 'email'];

    protected function name($value)
    {
        $this->request->validate(['name' => '

use Laimoon\UpdatableModel\Traits\Updateable;

class User extends Authenticatable
{
    use Updateable;
}

use App\Updaters\UserUpdater;

class UserController extends Controller
{
    public function store(UserUpdater $updates)
    {
        auth()->user()->fillUpdates($updates);
        
        return response()->json();
    }
}
bash
php artisan make:updater User