I saw that illuminate/database has a specific package for that illuminate/events
I have managed to make the observers work, but I can not access from the user's session to be able to register it in the table.
namespace Circe\Models;
use Illuminate\Database\Eloquent\Model;
class Events extends Model
{
protected $fillable = [
'users_id',
'name',
'day_event',
'description',
];
public static function boot()
{
parent::boot();
static::setEventDispatcher( new \Illuminate\Events\Dispatcher() );
static::observe(new ReadOnlyObserver());
}
}
class ReadOnlyObserver {
public function updating ($model, callable $func = null, $table = null, $id = null)
{
// Allow for overriding of table if it's not the model table
$table = $table ?: $model->getTable();
// Allow for overriding of id if it's not the model id
$id = $id ?: $model->id;
// Allow for customization of the history record if needed
$func = $func ?: [$this, 'getHistoryBody'];
var_dump($this->getUpdated($model)
->map(function ($value, $field) use ($func) {
return call_user_func_array($func, [$value, $field]);
}));
}
protected function getHistoryBody($value, $field)
{
return [
'body' => "Updated {$field} to ${value}",
];
}
protected function getUpdated($model)
{
return collect($model->getDirty())->filter(function ($value, $key) {
// We don't care if timestamps are dirty, we're not tracking those
return !in_array($key, ['created_at', 'updated_at']);
})->mapWithKeys(function ($value, $key) {
// Take the field names and convert them into human readable strings for the description of the action
// e.g. first_name -> first name
return [str_replace('_', ' ', $key) => $value];
});
}
}
How to access the user from the ReadOnlyObserver class?
Hello, I am adding a history to certain tables, to know what the users are doing.
https://medium.com/sammich-shop/simple-record-history-tracking-with-laravel-observers-48a2e3c5698b
I saw that illuminate/database has a specific package for that illuminate/events
I have managed to make the observers work, but I can not access from the user's session to be able to register it in the table.
How to access the user from the ReadOnlyObserver class?
Thanks for the help.