PHP Classes

File: src/Helpers/helpers.php

Recommend this page to a friend!
  Classes of Zacchaeus Bolaji   Laravel Revert Model Events   src/Helpers/helpers.php   Download  
File: src/Helpers/helpers.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Laravel Revert Model Events
Log, list and undo changes done on model objects
Author: By
Last change:
Date: 4 years ago
Size: 1,754 bytes
 

Contents

Class file image Download
<?php
/**
 * Created by PhpStorm.
 * User: Djunehor
 * Date: 10/6/2019
 * Time: 1:41 AM.
 */
if (! function_exists('model_event_revert')) {
    function
model_event_revert($log): array
    {
        try {
            switch (
$log->action) {
                case
'update':
                   
$data = json_decode($log->old, true);
                   
$model = $log->subject;
                    if (!
$model) {
                       
$response['message'] = trans('ModelEventLogger::model-event-logger.messages.modelNotExist');
                       
$response['status'] = false;
                    }
                   
$model->update($data);
                   
$response['status'] = true;
                    break;
                case
'create':
                   
$log->subject()->delete();
                   
$response['status'] = true;
                    break;
                case
'delete':
                   
$log->subject()->insert(json_decode($log->old, true));
                   
$response['status'] = true;
                    break;
                default:
                   
$response['status'] = false;
                   
$response['message'] = trans('ModelEventLogger::model-event-logger.messages.unknownModelEvent');
            }
        } catch (\
Exception $e) {
           
$response['status'] = false;
           
$response['message'] = 'An error occurred. Please check the logs.';

            \
Illuminate\Support\Facades\Log::info($e->getMessage());
        } catch (\
Throwable $e) {
           
$response['status'] = false;
           
$response['message'] = 'An error occurred. Please check the logs.';

            \
Illuminate\Support\Facades\Log::info($e->getMessage());
        }

        return
$response;
    }
}