Laravel Audit

composer require spatie/laravel-activitylog
php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="migrations"
php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="config"
php artisan config:cache
php artisan migrate
use Spatie\Activitylog\Traits\LogsActivity;
 
class User extends Authenticatable implements LdapAuthenticatable
{
//
    use LogsActivity;
 
    protected $fillable = [
        'name', 'username', 'pin', 'pin_expiry_date', 'mobile'
    ];
 
    //protected static $logFillable = true;
    // This would log all fillable.
    // But to reduce what we see in the log (an example below), we will use statically state exactly what we want to log.
    // {"attributes":{"name":"Richard Strong","username":"G12302","pin":"3680","pin_expiry_date":"2021-04-25T23:00:00.000000Z","mobile":"0851231234"},"old":{"name":"Richard Dunne","username":"G12302","pin":"3680","pin_expiry_date":"2021-04-25T23:00:00.000000Z","mobile":null}}
 
    protected static $logAttributes = ['pin', 'mobile'];
    // This gives the following detail in the properties field - which is much tidier that all values as in above example.
    // {"attributes":{"pin":"3680","mobile":"0851231235"},"old":{"pin":"3680","mobile":"0851231234"}}

Reference: https://docs.spatie.be/laravel-activitylog/v3/advanced-usage/logging-model-events/

    protected static $logOnlyDirty = true;
    protected static $submitEmptyLogs = false;
<?php
 
namespace App\Http\Controllers;
 
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Spatie\Activitylog\Models\Activity;
 
 
class AuditController extends Controller
{
    public function index(Activity $model, Request $request)
    {
        if (session('admin')) {
            $query = $request->q;
            $activities = Activity::with('subject', 'causer')->orderBy('created_at', 'desc')->paginate(10);
            return view('audit.index', ['query' => $query, 'activities' => $activities]);
 
        } else {
            Auth::logout();
            return redirect('/');
        }
    }
}
<table class="table">
	<thead class=" text-primary">
	<th>
		{{ __('Date Time') }}
	</th>
	<th>
		{{ __('Description') }}
	</th>
	<th>
		{{ __('Update By') }}
	</th>
	<th>
		{{ __('User Updated') }}
	</th>
	<th>
		{{ __('Changes') }}
	</th>
	</thead>
	<tbody>
	@foreach ($activities as $activity)
		<tr>
			<td>
				{{ $activity->created_at }}
			</td>
			<td>
				{{ $activity->description }}
			</td>
			<td>
				@isset($activity->causer->name)
					{{ $activity->causer->name }}
				@endisset
			</td>
			<td>
				@isset($activity->subject->name)
					{{ $activity->subject->name }}
				@endisset
			</td>
			<td>
				@isset($activity->changes)
					@foreach($activity->changes['attributes'] as $field => $value)
						| {{ @strtoupper($field) . ':' }}
						@isset($activity->changes['old'][$field])
							old:{{ $activity->changes['old'][$field] }} ,
						@endisset
						@isset($activity->changes['attributes'][$field])
							new:{{ $activity->changes['attributes'][$field] }}
						@endisset
					@endforeach
					|
				@endisset
			</td>
		</tr>
	@endforeach
	</tbody>
</table>