analytics backend update

This commit is contained in:
2023-05-01 19:04:08 +10:00
parent 0c668c9c62
commit cc0fe080cf
7 changed files with 352 additions and 2 deletions

View File

@@ -15,4 +15,31 @@ class Analytics extends Model
* @var array
*/
protected $guarded = [];
/**
* Create a new row in the analytics table with the given attributes,
* automatically assigning a session value based on previous rows.
*
* @param array $attributes Model attributes.
* @return static
*/
public static function createWithSession(array $attributes)
{
$previousRow = self::where('useragent', $attributes['useragent'])
->where('ip', $attributes['ip'])
->where('created_at', '>=', now()->subMinutes(30))
->whereNotNull('session')
->orderBy('created_at', 'desc')
->first();
if ($previousRow !== null) {
$attributes['session'] = $previousRow->session;
} else {
$lastSession = self::max('session');
$attributes['session'] = ($lastSession + 1);
}
return static::create($attributes);
}
}