PHP code example of reshadman / laravel-optimistic-locking

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

    

reshadman / laravel-optimistic-locking example snippets




class BlogPost extends Model {
    use OptimisticLocking;
}



$schema->integer('lock_version')->unsigned()->nullable();


\Reshadman\OptimisticLocking\StaleModelLockingException::class;



// Explicitly setting the lock version
class PostController {
    public function update($id)
    {
        $post = Post::findOrFail($id);
        $post->lock_version = request('lock_version');
        $post->save();
        // You can also define more implicit reusable methods in your model like Model::saveWithVersion(...$args); 
        // or just override the default Model::save(...$args); method which accepts $options
        // Then automatically read the lock version from Request and set into the model.
    }
}


$blogPost->disableLocking();
$blogPost->enableLocking();


class BlogPost extends \Illuminate\Database\Eloquent\Model 
{
    use \Reshadman\OptimisticLocking\OptimisticLocking;
    
    protected $lock = false;
}


class BlogPost extends \Illuminate\Database\Eloquent\Model
{
    use \Reshadman\OptimisticLocking\OptimisticLocking;
    
    /**
     * Name of the lock version column.
     *
     * @return string
     */
    protected static function lockVersionColumn()
    {
        return 'track_version';
    }
}


$query->where('id', $this->id)
    ->where('lock_version', $this->lock_version)
    ->update($changes);