djunehor/laravel-revert-query
Installation:
composer require djunehor/laravel-revert-query
Publish the migration and config:
php artisan vendor:publish --provider="Djunehor\RevertQuery\RevertQueryServiceProvider"
php artisan migrate
Enable Logging for a Model:
Add the Revertable trait to your model:
use Djunehor\RevertQuery\Traits\Revertable;
class User extends Model
{
use Revertable;
}
First Use Case:
Trigger an event (e.g., create, update, delete) on a model, then inspect the logs via:
// View all logs for a model
$logs = \Djunehor\RevertQuery\Facades\RevertQuery::logs('App\User', 1);
// Revert a specific log entry
$log = $logs->first();
$log->revert();
Logging Events Automatically:
The package hooks into Laravel’s model events (creating, created, updating, updated, deleting, deleted). No manual logging required—just use the Revertable trait.
Querying Logs: Use the facade to fetch logs for a model:
// Get logs for a specific model instance
$logs = \Djunehor\RevertQuery\Facades\RevertQuery::logs('App\User', $userId);
// Filter by event type (e.g., 'update')
$updateLogs = $logs->where('event', 'update');
Reverting Changes: Revert a log entry to its previous state:
$log = \Djunehor\RevertQuery\Facades\RevertQuery::logs('App\User', 1)->first();
$log->revert(); // Reverts the model to the state recorded in the log
Soft Deletes:
If your model uses soft deletes, ensure the Revertable trait is applied after SoftDeletes in the use statement to avoid conflicts.
Custom Events:
Extend the package to log custom events by publishing the config and adding your event names to config/revert-query.php under events.
'events' => [
'creating', 'created', 'updating', 'updated', 'deleting', 'deleted', 'custom_event',
],
Middleware for Admin Actions: Use middleware to restrict revert capabilities to admin users:
public function handle($request, Closure $next)
{
if ($request->user()->isAdmin()) {
return $next($request);
}
abort(403);
}
Batch Reverts: Revert multiple logs in a transaction for consistency:
\DB::transaction(function () {
$logs = \Djunehor\RevertQuery\Facades\RevertQuery::logs('App\User', 1)->limit(5);
foreach ($logs as $log) {
$log->revert();
}
});
Performance Overhead:
User or Order).Revertable trait or using the ignoreEvents config option.Soft Deletes Conflicts:
if (!$model->trashed) {
$log->revert();
}
Event Ordering:
delete before an update) may cause inconsistencies.Large Log Volumes:
foreach ($logs->chunk(100) as $chunk) {
RevertJob::dispatch($chunk);
}
Missing Logs:
Revertable trait is applied to the model.revert_query table exists and has data:
php artisan tinker
>> \Djunehor\RevertQuery\Models\RevertQueryLog::count();
Revert Failures:
old_data and new_data fields in the log to debug the state mismatch.config/revert-query.php:
'debug' => env('REVERT_QUERY_DEBUG', false),
Custom Storage: Override the default log storage (e.g., switch to Redis) by binding a custom repository:
$this->app->bind(
\Djunehor\RevertQuery\Repositories\RevertQueryRepository::class,
\App\Repositories\CustomRevertQueryRepository::class
);
Event Payloads:
Extend the logged data by overriding the getRevertableAttributes method in your model:
protected function getRevertableAttributes()
{
return array_merge(parent::getRevertableAttributes(), ['custom_field']);
}
Web Interface: Build a simple admin panel to view/revert logs using Laravel’s built-in tools:
// routes/web.php
Route::get('/revert-logs', [RevertLogController::class, 'index']);
// app/Http/Controllers/RevertLogController.php
public function index()
{
$logs = \Djunehor\RevertQuery\Facades\RevertQuery::all();
return view('revert-logs.index', compact('logs'));
}
Event Filtering: Exclude specific models or events from logging via config:
'ignore_models' => [
'App\LogEntry',
'App\CacheClearer',
],
'ignore_events' => [
'deleted', // Skip soft deletes
],
How can I help you explore Laravel packages today?