<?php
namespace QxCMS\Modules\Client\Models\Settings\UserLogs;
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
class UserLogs extends Model
{
protected $connection = 'client';
protected $table = 'users_logs';
protected $guarded = [];
protected $appends = ['hashid'];
public function getHashidAttribute()
{
return hashid($this->id);
}
public function saveLog($request)
{
$log = $this->fill($request);
$log->save();
$all_logs = $this->where('user_id', $this->attributes['user_id'])
->where('module_id', $this->attributes['module_id'])
->count();
$old_logs = $this->where('user_id', $this->attributes['user_id'])
->where('module_id', $this->attributes['module_id'])
->where('created_at', '<=', Carbon::now()->subDays(90));
if($all_logs - $old_logs->count() > 3) {
$old_logs->delete();
}
}
public function getCreatedAtAttribute($value)
{
return Carbon::parse($value)->format('M d, Y h:i A');
}
public function user()
{
return $this->belongsTo('QxCMS\Modules\Likod\Models\Clients\User', 'user_id');
}
public function module()
{
return $this->belongsTo('QxCMS\Modules\Likod\Models\Developer\ClientModule', 'module_id');
}
}