| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Models; |
| 4 |
|
| 5 |
class NotificationUser extends Model |
| 6 |
{ |
| 7 |
protected $table = 'fs_notification_users'; |
| 8 |
|
| 9 |
protected $fillable = [ |
| 10 |
'notification_id', |
| 11 |
'user_id', |
| 12 |
'channel', |
| 13 |
'is_read', |
| 14 |
'read_at' |
| 15 |
]; |
| 16 |
|
| 17 |
public static function boot() |
| 18 |
{ |
| 19 |
parent::boot(); |
| 20 |
|
| 21 |
static::creating(function ($model) { |
| 22 |
if (!$model->channel) { |
| 23 |
$model->channel = 'web'; |
| 24 |
} |
| 25 |
|
| 26 |
if ($model->is_read === null) { |
| 27 |
$model->is_read = 0; |
| 28 |
} |
| 29 |
|
| 30 |
$model->created_at = current_time('mysql'); |
| 31 |
$model->updated_at = current_time('mysql'); |
| 32 |
}); |
| 33 |
|
| 34 |
static::updating(function ($model) { |
| 35 |
$model->updated_at = current_time('mysql'); |
| 36 |
}); |
| 37 |
} |
| 38 |
|
| 39 |
public function notification() |
| 40 |
{ |
| 41 |
$class = __NAMESPACE__ . '\Notification'; |
| 42 |
|
| 43 |
return $this->belongsTo($class, 'notification_id', 'id'); |
| 44 |
} |
| 45 |
|
| 46 |
public function scopeUnread($query) |
| 47 |
{ |
| 48 |
return $query->where('is_read', 0); |
| 49 |
} |
| 50 |
|
| 51 |
public function scopeRead($query) |
| 52 |
{ |
| 53 |
return $query->where('is_read', 1); |
| 54 |
} |
| 55 |
} |
| 56 |
|