NotificationEvent::preferences()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
namespace Usamamuneerchaudhary\Notifier\Models;
3
4
use Illuminate\Database\Eloquent\Model;
5
use Illuminate\Database\Eloquent\Relations\HasMany;
6
use Illuminate\Support\Facades\Schema;
7
8
class NotificationEvent extends Model
9
{
10
    protected $table = 'notifier_events';
11
    protected $fillable = [
12
        'group',
13
        'name',
14
        'key',
15
        'description',
16
        'is_active',
17
        'settings',
18
    ];
19
20
    protected $casts = [
21
        'is_active' => 'boolean',
22
        'settings' => 'array',
23
    ];
24
25
    protected static function boot()
26
    {
27
        parent::boot();
28
29
        static::saving(function ($model) {
30
            // Check if settings column exists before trying to save
31
            try {
32
                $columns = Schema::getColumnListing($model->getTable());
33
                if (!in_array('settings', $columns)) {
34
                    // Remove settings from attributes if no column
35
                    unset($model->attributes['settings']);
36
                    unset($model->original['settings']);
37
                } elseif (empty($model->settings) || $model->settings === []) {
38
39
                    $model->settings = null;
40
                }
41
            } catch (\Exception $e) {
42
                // If we cant check, just unset it to be safe
43
                unset($model->attributes['settings']);
44
            }
45
        });
46
    }
47
48
    public function templates(): HasMany
49
    {
50
        return $this->hasMany(NotificationTemplate::class, 'event_key', 'key');
51
    }
52
53
    public function preferences(): HasMany
54
    {
55
        return $this->hasMany(NotificationPreference::class);
56
    }
57
}
58