I am developing Laravel 5 app. In Which I want to log Model create, update and deleted event with all New or changed(in case Model is being updated) Model Fields . I want simple reusable solution without writing too much of a code.
Laravel 5, log Model create, update , delete events
6.3k Views Asked by AudioBubble At
3
There are 3 best solutions below
1

Although old thread, but with specific reference to OP's request of simple reusable solution without writing too much of a code
, Spatie's activity logger is a breeze! This specific part of their full documentation shows how simply and easy OP's quest can be!
0

I've made a little modification hope It helps. ( this just keeps track of modification on the model )
migration
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateLogActions extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('log_actions', function (Blueprint $table) {
$table->increments('id');
$table->integer('loggable_id');
$table->string('loggable_type');
$table->string('action');
$table->integer('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->timestamp('dt_action');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('log_actions');
}
}
model
<?php
namespace App\Wdog\Entities;
use Illuminate\Database\Eloquent\Model;
use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;
class LogAction extends Model implements Transformable
{
use TransformableTrait;
protected $fillable = [
'loggable_type',
'loggable_id',
'user_id',
'action',
'dt_action'
];
public $timestamps = false;
protected $dates = [
'dt_action'
];
public function user()
{
return $this->belongsTo('App\User');
}
public function loggable()
{
return $this->morphTo();
}
}
Trait
finally just add a trait
<?php
namespace App\Wdog\Traits;
use App\Wdog\Entities\LogAction;
use Auth;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
trait ModelEventLogger
{
public static function bootModelEventLogger()
{
$user_id = Auth::user()->id;
foreach (static::getRecordActivityEvents() as $eventName) {
static::$eventName(function (Model $model) use ($user_id, $eventName) {
$log = new LogAction([
'user_id' => $user_id,
'dt_action' => Carbon::now(),
'action' => $eventName
]);
$model->logs()->save($log);
});
}
}
protected static function getRecordActivityEvents()
{
if (isset(static::$recordEvents)) {
return static::$recordEvents;
}
return [
'created',
'updated',
'deleted',
];
}
protected static function logs()
{
return $this->morphMany('\App\Wdog\Entities\LogAction', 'loggable')->orderBy('dt_action', 'desc');
}
}
then just use the trait in your model
class User extends Model implements Transformable
{
use ModelEventLogger;
...
Sure you can create trait like below and use it in all models you want to log.,
Now you can use this trait in any Model you want to throw events for. In your case in Article Model.
Ofcourse you will have to create 'Activity' model and related table with relevent columns. I hope this helps.