D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
forge
/
ghanempharmacy.com
/
app
/
Models
/
Filename :
Notification.php
back
Copy
<?php namespace App\Models; use App\Traits\StorageTrait; use Carbon\Carbon; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Support\Facades\DB; /** * Class Notification * * @property string $sent_by * @property string $sent_to * @property string $description * @property int $notification_count * @property string $image * @property int $status * @property Carbon $created_at * @property Carbon $updated_at */ class Notification extends Model { use StorageTrait; protected $fillable = [ 'customer_id', 'product_id', 'sent_by', 'sent_to', 'title', 'type', 'description', 'notification_count', 'image', 'status', 'read_at', 'link', ]; protected $casts = [ 'sent_by' => 'string', 'sent_to' => 'string', 'title' => 'string', 'type' => 'string', 'description' => 'string', 'notification_count' => 'integer', 'image' => 'string', 'status' => 'integer', 'read_at' => 'datetime', 'link' => 'string', 'created_at' => 'datetime', 'updated_at' => 'datetime', ]; public function scopeActive($query): mixed { return $query->where('status', 1); } /* notification_seen_by -> notificationSeenBy */ public function notificationSeenBy(): HasOne { return $this->hasOne(NotificationSeen::class, 'notification_id')->where('user_id', auth('customer')->id()); } public function getIsReadAttribute(): bool { return $this->read_at !== null || $this->notificationSeenBy !== null; } public function customer(): HasOne { return $this->hasOne(User::class, 'id', 'customer_id'); } public function product(): BelongsTo { return $this->belongsTo(Product::class, 'product_id'); } public function getImageFullUrlAttribute(): array|null { $value = $this->image; if (count($this->storage) > 0) { $storage = $this->storage->where('key', 'image')->first(); } return $this->storageLink('notification', $value, $storage['value'] ?? 'public'); } protected $appends = ['image_full_url']; protected static function boot(): void { parent::boot(); static::saved(function ($model) { if ($model->isDirty('image')) { $storage = config('filesystems.disks.default') ?? 'public'; DB::table('storages')->updateOrInsert([ 'data_type' => get_class($model), 'data_id' => $model->id, 'key' => 'image', ], [ 'value' => $storage, 'created_at' => now(), 'updated_at' => now(), ]); } }); } }