. */ namespace Poniverse\Ponyfm\Models; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; /** * Poniverse\Ponyfm\Models\Notification * * @property integer $id * @property integer $activity_id * @property integer $user_id * @property boolean $is_read * @property-read \Poniverse\Ponyfm\Models\Activity $activity * @property-read \Poniverse\Ponyfm\Models\Email $email * @property-read \Poniverse\Ponyfm\Models\User $recipient * @method static \Illuminate\Database\Query\Builder|\Poniverse\Ponyfm\Models\Notification forUser($user) * @method static \Illuminate\Database\Query\Builder|\Poniverse\Ponyfm\Models\Notification whereId($value) * @method static \Illuminate\Database\Query\Builder|\Poniverse\Ponyfm\Models\Notification whereActivityId($value) * @method static \Illuminate\Database\Query\Builder|\Poniverse\Ponyfm\Models\Notification whereUserId($value) * @method static \Illuminate\Database\Query\Builder|\Poniverse\Ponyfm\Models\Notification whereIsRead($value) * @mixin \Eloquent */ class Notification extends Model { public $timestamps = false; protected $fillable = ['activity_id', 'user_id']; protected $casts = [ 'id' => 'integer', 'activity_id' => 'integer', 'user_id' => 'integer', 'is_read' => 'boolean', ]; public function activity() { return $this->belongsTo(Activity::class, 'activity_id', 'id'); } public function recipient() { return $this->belongsTo(User::class, 'user_id', 'id'); } public function email() { return $this->hasOne(Email::class, 'notification_id', 'id'); } /** * This scope grabs eager-loaded notifications for the given user. * * @param Builder $query * @param User $user * @return Builder */ public function scopeForUser(Builder $query, User $user) { $result = $query->with([ 'activity', 'activity.initiatingUser', 'activity.resource', ]) ->join('activities', 'notifications.activity_id', '=', 'activities.id') ->where('notifications.user_id', $user->id) ->whereNull('activities.deleted_at') ->select('*', 'notifications.id as id') ->orderBy('activities.created_at', 'DESC'); return $result; } public function toArray() { if (is_null($this->activity->resource)) { return ''; } return [ 'id' => $this->id, 'date' => $this->activity->created_at->toAtomString(), 'thumbnail_url' => $this->activity->thumbnail_url, 'text' => $this->activity->text, 'url' => $this->activity->url, 'is_read' => $this->is_read ]; } }