Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rjruiz's avatar

Audit with laravel auditing

Greetings I am desperate to find a solution that will result. I am implementing the package owen-it / laravel-auditing. I need to show the data in the old_values and new_values fields. In my blade file I managed to show the data belonging to the old_values field but I cannot display the data belonging to the new values field. Then I expose my code Please can you help me I just need to show the new_values field. These fields old_values and new_values are of type text

Model Post:

<?php

namespace App;

use App\Tag;
use App\Category;
use Carbon\Carbon;


use Illuminate\Database\Eloquent\Model;
use OwenIt\Auditing\Contracts\Auditable;

class Post extends Model implements Auditable
{
    use \OwenIt\Auditing\Auditable;

    protected $fillable = ['user_id', 'category_id', 'title', 'body', 'iframe', 'excerpt', 'published_at'];

    protected $auiditInclude = ['user_id', 'category_id', 'title', 'body', 'published_at'];
    

AuditController:

<?php

namespace App\Http\Controllers;

use App\Post;
use Illuminate\Http\Request;

class AuditController extends Controller
{
    public function index()
    {
        $audits = \OwenIt\Auditing\Models\Audit::with('user')
                ->orderBy('created_at', 'desc')                
                ->get();
             
        return view('admin.audits.index', ['audits' => $audits]);       
    }
     
}


file blade:

@extends('layouts.app')

@section('header')        
    <h1>
        AUDITORIA
        <small>Listado </small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="{{ route('dashboard') }}"><i class="fa fa-dashboard"></i> Inicio</a></li>
        <li class="active">Auditoria</li>
    </ol> 
@stop

@section('content')
    <div class="panel panel-primary">        
        <div class="panel-heading">
            <h3 class="panel-title">Listado de las acciones de los usuarios</h3>
        </div>
        <div class="panel-body">
            <table id="datatable" class="table table-bordered table-hover" style="width:100%">
                <thead class="thead-dark">
                    <tr>
                        <th>Model</th>
                        <th>Action</th>
                        <th>User</th>
                        <th>Time</th>
                        <th>Old Values</th>       
                        <th>New Values</th>
                        <th>Url</th>
                        <th>Ip_adrress</th>       
                        <th>Navegador</th>                       
                    </tr>
                </thead>
                <tbody id="audits">
                    @foreach($audits as $audit)                        
                        <tr>
                            <td>{{ $audit->auditable_type }} (id: {{ $audit->auditable_id }})</td>
                            <td>{{ $audit->event }}</td>
                            <td>{{ $audit->user->name }}</td>
                            <td>{{ $audit->created_at }}</td>                            
                            <td>
                                <table class="table table-bordered table-hover" style="width:100%">
                                    @foreach($audit->old_values as $attribute  => $value)                                 
                                        <tr>
                                            <td><b>{{ $attribute  }}</b></td>
                                            <td>{{ $value }}</td>
                                        </tr>                                  
                                    @endforeach
                                </table>
                            </td>                    
                            <td>
                                <table class="table table-bordered table-hover" style="width:100%">
                                    @foreach($audit->new_values as  $attribute  => $data)
                                        <tr>
                                            <td><b>{{  $attribute  }}</b></td>
                                            <td>{{ $value }}</td>
                                        </tr>
                                    @endforeach
                                </table>
                            </td>
                            <td>{{ $audit->url }}</td>
                            <td>{{ $audit->ip_address }}</td>
                            <td>{{ $audit->user_agent }}</td>                                              
                        </tr>
                    @endforeach
                </tbody>                
            </table>
        </div>
    </div> 
@stop

@push('styles')      
    <link rel="stylesheet" href="/adminlte/bower_components/datatables.net-bs/css/dataTables.bootstrap.min.css">  
@endpush

@push('scripts')  
    <script src="/adminlte/bower_components/datatables.net/js/jquery.dataTables.min.js"></script>
    <script src="/adminlte/bower_components/datatables.net-bs/js/dataTables.bootstrap.min.js"></script>    
    
    <script>
        $('#datatable').DataTable({
            
        });
    </script>
@endpush

The old_values field has this information in my database:

{"title":"my fourth post "}

And the new_values field has this information in my database:

{"title":"my fourth post edited "}

Exclusive laravel auditing package model:

<?php

namespace OwenIt\Auditing\Models;

use Illuminate\Database\Eloquent\Model;

class Audit extends Model implements \OwenIt\Auditing\Contracts\Audit
{
    use \OwenIt\Auditing\Audit;

    /**
     * {@inheritdoc}
     */
    protected $guarded = [];

    /**
     * {@inheritdoc}
     */
    protected $casts = [
        'old_values'   => 'array',
        'new_values'   => 'array',
        'auditable_id' => 'integer',
    ];
    // protected $casts = [
    //     'old_values'   => 'json',
    //     'new_values'   => 'json',
    //     'auditable_id' => 'integer',
    // ];
}

I read the documentation: http://www.laravel-auditing.com/docs/9.0/introduction but there are things I do not understand why I am asking for help

0 likes
1 reply

Please or to participate in this conversation.