Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
code:laravel:audit [2020/04/26 11:50] – gerardorourkecode:laravel:audit [2020/04/29 18:45] (current) – [Log only items which have changed] gerardorourke
Line 13: Line 13:
 </code> </code>
  
 +==== Logging Model Events ====
  
 +<code php>
 +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"}}
 +</code>
 +
 +==== Log only items which have changed ====
 +Reference: https://docs.spatie.be/laravel-activitylog/v3/advanced-usage/logging-model-events/
 +<code>
 +    protected static $logOnlyDirty = true;
 +    protected static $submitEmptyLogs = false;
 +</code>
 +
 +===== Audit Controller Code =====
 +<code php>
 +<?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('/');
 +        }
 +    }
 +}
 +</code>
 +
 +===== Extract of Blade Code =====
 +<code php>
 +<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>
 +</code>
  • code/laravel/audit.1587898218.txt.gz
  • Last modified: 2020/04/26 11:50
  • by gerardorourke