Laravel-Auditing is not working without any errors

Viewed 705

I've recently installed this package and configured everything with guide but some how it's not working! By it's not working I mean it's not adding anything to database. I really don't know what is wrong with my configs but I've checked everything with guide 3 times and everything is correct but... I don't know

config/audit.php:

<?php

return [

    'enabled' => env('AUDITING_ENABLED', true),

    'implementation' => OwenIt\Auditing\Models\Audit::class,

    'user' => [
        'morph_prefix' => 'user',
        'guards'       => [
            'web',
            'api',
        ],
    ],

    'resolver' => [
        'user'       => OwenIt\Auditing\Resolvers\UserResolver::class,
        'ip_address' => OwenIt\Auditing\Resolvers\IpAddressResolver::class,
        'user_agent' => OwenIt\Auditing\Resolvers\UserAgentResolver::class,
        'url'        => OwenIt\Auditing\Resolvers\UrlResolver::class,
    ],

    'events' => [
        'created',
        'updated',
        'deleted',
        'restored',
        'gold_mailed' => 'goldMailed',
        'invited' => 'clientInvited',
    ],

    'strict' => false,

    'timestamps' => false,

    'threshold' => 0,

    'driver' => 'session',

    'drivers' => [
        'eloquent' => [
            'table'      => 'audits',
            'connection' => null,
        ],
    ],

    'console' => true,
];

My model that I want to audit:

<?php

namespace App\Models;

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

use App\Models\Expansion;
use App\Models\Audit;

class Setting extends Model implements Auditable
{
    protected $table = 'settings';

    use \OwenIt\Auditing\Auditable;


    protected $fillable = [
      'expansion_id', 'season', 'advertiser_app', 'pvp_app', 'raid_app', 'version'
    ];

    protected $auditInclude = [
       'expansion_id', 'season', 'advertiser_app', 'pvp_app', 'raid_app', 'version'
   ];

    public function Expansion()
    {
      return $this->hasOne(Expansion::class, 'id', 'expansion_id');
    }
}

web.php:

Route::post('/setting' , 'Admin\SuperAdminController@saveSetting')->middleware('superadmin')->name('admin_save_setting');

Controller:

public function saveSetting(Request $request)
  {
    $sql = Setting::where('id', 1)->update([
      'expansion_id' => $request['expansion_id'],
      'season' => $request['season'],
      'advertiser_app' => $request['advertiser_app'],
      'pvp_app' => $request['pvp_app'],
      'raid_app' => $request['raid_app'],
      'version' => $request['version']
    ]);
    if ($sql) {
      toastr()->success('Settings successfully updated.');
      return redirect()->back();
    }
    toastr()->error('Something went wrong!');
    return redirect()->back();
  }

I don't know what infos do you need but I think this is enough

I think my problem is with "driver" in config file , I don't know if that's correct or not

2 Answers

[UPDATED]

Based on the controller code you showed, it didn't work because your code is being called using Builder style, and the package only works when it is called using Eloquent style.

Documentation link Builder vs. Eloquent

So, maybe you need to change your code to:

$setting = Setting::where('id', 1)->firstOrFail();
$setting->update([
  'expansion_id' => $request['expansion_id'],
  'season' => $request['season'],
  'advertiser_app' => $request['advertiser_app'],
  'pvp_app' => $request['pvp_app'],
  'raid_app' => $request['raid_app'],
  'version' => $request['version']
]);

now I have another problem -_-

this is my controller:

$sql = Raid::findOrFail($request['id']);
$sql = $sql->update($request->all());

I have a array in my table , after update value will be like this:

"{\"Plate\":0,\"Cloth\":0,\"Mail\":0,\"Leather\":0}"

but it should be:

{"Plate":"0","Cloth":"0","Mail":"0","Leather":"0"}

so I will get an error before this , I was updating like this and it was ok:

$sql = Raid::where('id', $request['id'])->update($request->all());

and this is my mode (traders and class_traders is fields that I have problem with):

    use SoftDeletes;

    use \OwenIt\Auditing\Auditable;

    protected $table = 'raid';

    protected $dates = ['date_and_time','deleted_at'];

    protected $fillable = [
      'admin_id', '....
    ];

    protected $casts = [
        'bosses' => 'array',
        'traders' => 'array',
        'class_traders' => 'array',
        'boosters' => 'array',
    ];
Related