<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; /** * @method static where(string $string, string $uid) * @method static create(array $array) */ class Notifications extends Model { protected $fillable = [ 'user_id', 'notification_for', 'notification_type', 'message', 'mark_read', ]; /** * Bootstrap any application services. */ public static function boot() { parent::boot(); // Create uid when creating list. static::creating(function ($item) { // Create new uid $uid = uniqid(); while (self::where('uid', $uid)->count() > 0) { $uid = uniqid(); } $item->uid = $uid; }); } /** * get user * * @return BelongsTo * */ public function user(): BelongsTo { return $this->belongsTo(User::class); } /** * get route key by uid * * @return string */ public function getRouteKeyName(): string { return 'uid'; } /** * @return string */ public function __toString(): string { return $this->name; } }