PHP code example of kerwin-cn / laravel-optimistic-locking
1. Go to this page and download the library: Download kerwin-cn/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.
<?phprequire_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
kerwin-cn / laravel-optimistic-locking example snippets
// Explicitly setting the lock versionclassPostController{
publicfunctionupdate($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.
}
}
classBlogPostextends \Illuminate\Database\Eloquent\Model{
use \Reshadman\OptimisticLocking\OptimisticLocking;
protected $lock = false;
}
classBlogPostextends \Illuminate\Database\Eloquent\Model{
use \Reshadman\OptimisticLocking\OptimisticLocking;
/**
* Name of the lock version column.
*
* @return string
*/protectedstaticfunctionlockVersionColumn(){
return'track_version';
}
}