Getting Started
Installation
Give the application access to the package using the PHP Packages application, then install it using Composer:
scripts/composer.sh require alberon/nexus-record-locking
Usage
Add the LockableRecord trait to any Eloquent models that will be locked.
use Alberon\NexusRecordLocking\Models\LockableRecord;
class Example extends Model
{
use LockableRecord;
// ...
}
Add a call to lockRecord() at the start of each edit() method to obtain a lock. You also need to pass the lock_key property to Vue.
public function edit(Example $example)
{
if (!$example->lockRecord()) {
return redirect()->route('examples.show', $example);
}
return Inertia::render('Examples/PageEdit', [
// ...
'lockKey' => $example->lock_key,
]);
}
Note: It must be at the top level and named lockKey, as that's what the Vue mixin expects.
Add the same if (!$example->lockRecord()) { ... } block to the destroy() method and any custom methods that update the model.
On the edit page you need to:
- Add the
RecordLockingmixin - Disable saving and display
<AlertRecordLockLost>(or a custom message) ifrecordLocking_isLockedbecomes false
<script>
import RecordLocking from '@alberon/nexus-record-locking/vue/mixins/RecordLocking';
import AlertRecordLockLost from '@alberon/nexus-record-locking/vue/AlertRecordLockLost';
export default {
mixins: [ RecordLocking ],
components: {
AlertRecordLockLost,
// ...
},
}
</script>
<template>
<LayoutMain>
<template #alerts>
<AlertRecordLockLost v-if="!recordLocking_isLocked" />
</template>
...
<ButtonSave :disabled="!recordLocking_isLocked" />
</LayoutMain>
</template>
The mixin will automatically refresh the lock every 10 seconds while the page is open, release it when they leave (as long as it's via an Inertia link), and also try to prevent the user from closing the tab (using the beforeunload event).
Once the model has been saved, call unlockRecord() to ensure the lock is released before you redirect them to another page:
public function update(Example $example, ExampleRequest $request)
{
$example->update($request->validated());
$example->unlockRecord();
return redirect()->route('examples.show', $example)
->with('alert-success', "{$example->name} saved.");
}
(Don't do this if you are redirecting back to the edit page.)