PHP code example of thtg88 / journalism

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

    

thtg88 / journalism example snippets



// app/Providers/AppServiceProvider.php

use Thtg88\Journalism\Helpers\JournalEntryHelper;

public function register(): void
{
    $this->app->singleton(JournalEntryHelper::class, static function ($app) {
        return $app->make(JournalEntryHelper::class);
    });
}

use Thtg88\Journalism\Helpers\JournalEntryHelper;

(new JournalEntryHelper())->createJournalEntry('create', $model, ['foo' => 'bar']);



// app/Observers/Observer.php

namespace App\Observers;

use Illuminate\Database\Eloquent\Model;
use Thtg88\Journalism\Helpers\JournalEntryHelper;
use Thtg88\Journalism\Models\JournalEntry;

abstract class Observer
{
    /**
     * Handle the Model "created" event.
     *
     * @param \Illuminate\Database\Eloquent\Model $model
     * @return void
     */
    public function created(Model $model): void
    {
        if (config('journalism.enabled') === false) {
            return;
        }

        // Create journal entry only if not creating journal entry i.e. infinite recursion
        if ($model instanceof JournalEntry) {
            return;
        }

        app(JournalEntryHelper::class)->createJournalEntry(
            'create',
            $model,
            $model->toArray(),
        );
    }

    /**
     * Handle the Model "updated" event.
     *
     * @param \Illuminate\Database\Eloquent\Model $model
     * @return void
     */
    public function updated(Model $model): void
    {
        if (config('journalism.enabled') === false) {
            return;
        }

        // Create journal entry only if not creating journal entry i.e. infinite recursion
        if ($model instanceof JournalEntry) {
            return;
        }

        app(JournalEntryHelper::class)->createJournalEntry(
            'update',
            $model,
            $model->toArray(),
        );
    }

    /**
     * Handle the Model "deleted" event.
     *
     * @param \Illuminate\Database\Eloquent\Model $model
     * @return void
     */
    public function deleted(Model $model): void
    {
        if (config('journalism.enabled') === false) {
            return;
        }

        // Create journal entry only if not creating journal entry i.e. infinite recursion
        if ($model instanceof JournalEntry) {
            return;
        }

        app(JournalEntryHelper::class)->createJournalEntry('delete', $model);
    }

    /**
     * Handle the Model "forceDeleted" event.
     *
     * @param \Illuminate\Database\Eloquent\Model $model
     * @return void
     */
    public function forceDeleted(Model $model): void
    {
        if (config('journalism.enabled') === false) {
            return;
        }

        // Create journal entry only if not creating journal entry i.e. infinite recursion
        if ($model instanceof JournalEntry) {
            return;
        }

        app(JournalEntryHelper::class)->createJournalEntry('delete', $model);
    }
}



// app/Observers/UserObserver.php

namespace App\Observers;

class UserObserver extends Observer
{
}

use App\Models\User;
use App\Observers\UserObserver;

public function boot()
{
    User::observe(UserObserver::class);
}
bash
php artisan vendor:publish --provider="Thtg88\Journalism\JournalismServiceProvider"