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 12:01] – [Logging Model Events] gerardorourkecode:laravel:audit [2020/04/29 18:45] (current) – [Log only items which have changed] gerardorourke
Line 35: Line 35:
     // This gives the following detail in the properties field - which is much tidier that all values as in above example.     // 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"}}     // {"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>
  • code/laravel/audit.1587898865.txt.gz
  • Last modified: 2020/04/26 12:01
  • by gerardorourke