/nova-heartbeat-resource-field

Heartbeat for resources to block editing same resource with 2 or more people

Primary LanguagePHPMIT LicenseMIT

NovaHeartbeatResourceField

Heartbeat Resource Field

This package provides a custom Nova field that will like heartbeat monitor watch who is editing currently the resource.

On a listing/details there will be information who is currently editing and as well another person will be blocked from editing the same resource to not overlap changes.

Package is made for Laravel Nova ^4.

Installation

You can install the package via composer:

composer require mateusz-peczkowski/nova-heartbeat-resource-field

Migrations

After installation, you need to run the migration to create the table that will store the heartbeat data.

php artisan migrate

Publish package (optional)

Publish the configuration file to customize the settings of the package (check bellow for more details).

php artisan vendor:publish --provider="MateuszPeczkowski\NovaHeartbeatResourceField\HeartbeatResourceServiceProvider"

Configuration

The package provides a configuration file that allows you to customize the settings of the package.

return [
    'table_name'            => 'nova_heartbeats',
    'heartbeat_model'       => \MateuszPeczkowski\NovaHeartbeatResourceField\Models\HeartbeatResource::class,
    'heartbeat_interval'    => env('NOVA_HEARTBEAT_INTERVAL', 5 * 1000), // 5 seconds
    'heartbeat_timeout'     => env('NOVA_HEARTBEAT_TIMEOUT', 60 * 1000), // 1 minute
    'heartbeat_guard'       => 'web',
    'heartbeat_guard_name'  => 'name',
    'heartbeat_guard_email' => 'email',
    'heartbeat_avatar_url'  => null,
];

Usage

Nova Resource Field

To install this field in your Nova resource, you need to add the following code to the fields method of your resource.

use MateuszPeczkowski\NovaHeartbeatResourceField\NovaHeartbeatResourceField;

NovaHeartbeatResourceField::make('Heartbeat')
    ->resourceId($this->id),

Optionally you can allow to retake the resource by adding the following code

use MateuszPeczkowski\NovaHeartbeatResourceField\NovaHeartbeatResourceField;

NovaHeartbeatResourceField::make('Heartbeat')
    ->resourceId($this->id)
    ->allowRetake(),

Then on details view you will have additional button to retake the resource.

Nova Resource Trait

Add this trait to your Nova resource

use MateuszPeczkowski\NovaHeartbeatResourceField\Traits\HasNovaHeartbeats;

class YourResource extends Resource
{
    use HasNovaHeartbeats;
}

Model Trait

Add this trait to your Model

use MateuszPeczkowski\NovaHeartbeatResourceField\Traits\HasHeartbeats;

class YourModel extends Model
{
    use HasHeartbeats;
}

Clearing expired heartbeats (recommended)

In case of failure of removing the heartbeat, you can use the following command to remove all the heartbeats that are older than the timeout.

$schedule->command('heartbeat:clear-expired')->everyMinute();