Skip to main content
Version: 1.1.0

Creating a custom Exception Model

If you want to add new features or improve the original MoonGuard ExceptionLog model, we allow you to create custom models of these for your project doing the following steps.

  1. Create a new ExceptionLog class that extends from  Illuminate\Database\Eloquent\Model and implements the  Taecontrol\MoonGuard\Contracts\MoonGuardExceptionLog interface.

    <?php

    use Illuminate\Database\Eloquent\Model;
    use Taecontrol\MoonGuard\Contracts\MoonGuardExceptionLog;

    class ExceptionLog extends Model implements MoonGuardExceptionLog
    {
    // Contract implementation
    }
  2. Implement all the properties and methods required, you can guide yourself with the model reference.

  3. Replace the new ExceptionLog model class in the configuration file.

    <?php
    [
    'exceptions' => [
    //...
    'exception_log' => [
    /*
    * The exception log model to use.
    */
    'model' => \Taecontrol\MoonGuard\Models\ExceptionLog::class,
    ],

    'exception_log_group' => [
    /*
    * The exception log group model to use.
    */
    'model' => \Taecontrol\MoonGuard\Models\ExceptionLogGroup::class,
    ],
    ]
    ]

In case you want to re implement the ExceptionLogGroup model you can use the guide of ExceptionLog model and implement Taecontrol\MoonGuard\Contracts\MoonGuardExceptionLogGroup and ExceptionLogGroup.php

Please check the model reference for more information.

Model Reference

ExceptionLog model

<?php

namespace Taecontrol\MoonGuard\Models;

use Illuminate\Database\Eloquent\Model;
use Taecontrol\MoonGuard\Enums\ExceptionLogStatus;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Taecontrol\MoonGuard\Repositories\SiteRepository;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
use Taecontrol\MoonGuard\Contracts\MoonGuardExceptionLog;
use Taecontrol\MoonGuard\Repositories\ExceptionLogGroupRepository;

class ExceptionLog extends Model implements MoonGuardExceptionLog
{
use HasFactory;

protected $fillable = [
'message',
'type',
'file',
'status',
'trace',
'request',
'line',
'thrown_at',
'exception_log_group_id',
];

protected $casts = [
'status' => ExceptionLogStatus::class,
'trace' => 'array',
'request' => 'array',
'thrown_at' => 'immutable_datetime',
];

public function site(): HasOneThrough
{
return $this->hasOneThrough(
SiteRepository::resolveModelClass(),
ExceptionLogGroupRepository::resolveModelClass()
);
}

public function exceptionLogGroup(): BelongsTo
{
return $this->belongsTo(ExceptionLogGroupRepository::resolveModelClass());
}
}

Fillable fields

NameDescription
messageThe error message associated with the exception
typeThe type of the exception
fileThe file where the exception occurred
statusThe status of the exception log
traceThe stack trace of the exception
requestThe HTTP request associated with the exception
lineThe line number where the exception occurred
thrown_atThe date and time when the exception was thrown
exception_log_group_idThe ID of the exception log group that this exception log belongs to

Casts

NameDescription
statusThe status of the exception log, casted to the ExceptionLogStatus enum
traceThe stack trace of the exception, casted to an array
requestThe HTTP request associated with the exception, casted to an array
thrown_atThe date and time when the exception was thrown, casted to an immutable datetime object

Methods

Method NameReturn TypeDescription
site()HasOneThroughReturns a HasOneThrough relationship between the ExceptionLog model and the Site model through the ExceptionLogGroup model. This method allows you to retrieve the site associated with the exception log.
exceptionLogGroup()BelongsToReturns a BelongsTo relationship between the ExceptionLog model and the ExceptionLogGroup model. This method allows you to retrieve the exception log group that this exception log belongs to.

ExceptionLogGroup model

<?php

namespace Taecontrol\MoonGuard\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Taecontrol\MoonGuard\Repositories\SiteRepository;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Taecontrol\MoonGuard\Repositories\ExceptionLogRepository;
use Taecontrol\MoonGuard\Contracts\MoonGuardExceptionLogGroup;

class ExceptionLogGroup extends Model implements MoonGuardExceptionLogGroup
{
use HasFactory;

protected $fillable = [
'site_id',
'message',
'type',
'file',
'line',
'first_seen',
'last_seen',
];

protected $casts = [
'first_seen' => 'immutable_datetime',
'last_seen' => 'immutable_datetime',
];

public function exceptionLogs(): HasMany
{
return $this->hasMany(ExceptionLogRepository::resolveModelClass());
}

public function site(): BelongsTo
{
return $this->belongsTo(SiteRepository::resolveModelClass());
}
}

Fillable fields

NameDescription
site_idThe ID of the site associated with the exception log group
messageThe error message associated with the exception
typeThe type of the exception
fileThe file where the exception occurred
lineThe line number where the exception occurred
first_seenThe date and time when the exception was first seen
last_seenThe date and time when the exception was last seen

Casts

Column NameDescription
first_seenThe date and time when the exception was first seen, casted to an immutable datetime object
last_seenThe date and time when the exception was last seen, casted to an immutable datetime object

Methods

Method NameReturn TypeDescription
exceptionLogs()HasManyReturns a HasMany relationship between the ExceptionLogGroup model and the ExceptionLog model. This method allows you to retrieve the exception logs associated with this exception log group.
site()BelongsToReturns a BelongsTo relationship between the ExceptionLogGroup model and the Site model. This method allows you to retrieve the site associated with this exception log group.