| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\App\Models; |
| 4 |
|
| 5 |
use FluentCommunity\App\Models\XProfile; |
| 6 |
|
| 7 |
/** |
| 8 |
* Meta Model - DB Model for Notifications table |
| 9 |
* |
| 10 |
* Database Model |
| 11 |
* |
| 12 |
* @package FluentCommunity\App\Models |
| 13 |
* |
| 14 |
* @version 1.0.0 |
| 15 |
*/ |
| 16 |
class NotificationSubscriber extends Model |
| 17 |
{ |
| 18 |
protected $table = 'fcom_notification_users'; |
| 19 |
|
| 20 |
protected $primaryKey = 'id'; |
| 21 |
|
| 22 |
protected $guarded = ['id']; |
| 23 |
|
| 24 |
protected $fillable = [ |
| 25 |
'object_id', |
| 26 |
'user_id', |
| 27 |
'is_read', |
| 28 |
'object_type', |
| 29 |
'notification_type' |
| 30 |
]; |
| 31 |
|
| 32 |
protected static function boot() |
| 33 |
{ |
| 34 |
parent::boot(); |
| 35 |
|
| 36 |
static::creating(function ($model) { |
| 37 |
$model->object_type = 'notification'; |
| 38 |
$model->notification_type = 'web'; |
| 39 |
}); |
| 40 |
|
| 41 |
static::updating(function ($model) { |
| 42 |
$model->object_type = 'notification'; |
| 43 |
$model->notification_type = 'web'; |
| 44 |
}); |
| 45 |
|
| 46 |
// Add global scope to get only notifications |
| 47 |
static::addGlobalScope('notification', function ($builder) { |
| 48 |
$builder->where('object_type', 'notification'); |
| 49 |
}); |
| 50 |
} |
| 51 |
|
| 52 |
public function user() |
| 53 |
{ |
| 54 |
return $this->belongsTo(User::class, 'user_id'); |
| 55 |
} |
| 56 |
|
| 57 |
public function xprofile() |
| 58 |
{ |
| 59 |
return $this->belongsTo(XProfile::class, 'user_id', 'user_id'); |
| 60 |
} |
| 61 |
|
| 62 |
public function notification() |
| 63 |
{ |
| 64 |
return $this->belongsTo(Notification::class, 'object_id'); |
| 65 |
} |
| 66 |
|
| 67 |
public function scopeUnread($query) |
| 68 |
{ |
| 69 |
return $query->where('is_read', 0); |
| 70 |
} |
| 71 |
|
| 72 |
public function scopeRead($query) |
| 73 |
{ |
| 74 |
return $query->where('is_read', 1); |
| 75 |
} |
| 76 |
|
| 77 |
} |
| 78 |
|