PHP code example of quarks / laravel-locking

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

    

quarks / laravel-locking example snippets


/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::table('blog_posts', function (Blueprint $table) {
        // create column for version tracking
        $table->lockVersion();
        // or to use a custom column name e.g., lock_version
        $table->lockVersion('lock_version');
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('blog_posts', function (Blueprint $table) {
        $table->dropLockVersion(); // or $table->dropLockVersion('lock_version');
    });
}

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Quarks\Laravel\Locking\LocksVersion;

class BlogPost extends Model
{
    use LocksVersion;
    
    /**
     * Override the default lock version column name, optional.
     */
    protected static function lockVersionColumnName()
    {
        return 'lock_version';
    }
}

namespace App\Http\Controllers;

use Quarks\Laravel\Locking\LockedVersionMismatchException;

// ... other imports

class BlogPostController extends Controller
{

    // ... more methods

    public function update(BlogPost $blogPost, BlogPostRequest $request)
    {
        $data = $request->validated();
        $blogPost->fill($data);
        $blogPost->fillLockVersion();

        try {
            $blogPost->save();
        } catch (LockedVersionMismatchException $e) {
            abort(409, 'This model was already modified elsewhere.');
        }
    }
}
html
<form method="post">
    @lockInput($blogPost)
    
    <!-- more fields -->
</form>