Edit File: UserHelpers.php
<?php namespace App\Models\Helpers; use App\Enums\SubscriptionType; use App\Http\Resources\Api\UserResource; use Carbon\Carbon; trait UserHelpers { /** * Get the resource for user. * * @return UserResource */ public function getResource() { return new UserResource($this); } /** * Get the current device */ public function getCurrentDeviceAttribute() { return $this->devices()->where('is_current', true)->first(); } /** * Get the num of friends */ public function getNumOfFriendsAttribute() { return $this->friends()->count(); } /** * Get the num of received likes */ public function getNumOfLikesAttribute() { return $this->receivedLikes()->count(); } /** * Get the full name * @return string * */ public function getFullNameAttribute() { return $this->first_name . ' ' . $this->last_name; } /** * Get the gender text * @return string * */ public function getGenderTextAttribute() { if (is_null($this->gender)) { return null; } return $this->gender === 'male' ? __('admin.male') : __('admin.female'); } /** * Get the user's age. * * @return int|null */ public function getAgeAttribute() { if ($this->birth_date) { return Carbon::parse($this->birth_date)->age; } return null; } public function getFriendStatusAttribute() { if (!auth()->check()) return 'none'; $authUser = auth()->user(); if ($authUser->isFriend($this->id)) { return 'friend'; } if ($authUser->sentFriendRequests()->where('friend_id', $this->id)->pending()->exists()) { return 'sent'; } if ($authUser->receivedFriendRequests()->where('user_id', $this->id)->pending()->exists()) { return 'received'; } return 'none'; } public function getCoverAttribute() { if (isset($this->attributes['cover']) && $this->attributes['cover']) { $image = $this->getImage($this->attributes['cover'], static::IMAGEPATH); } else { $image = $this->defaultImage(static::IMAGEPATH); } return $image; } public function setCoverAttribute($value) { if (null != $value && is_file($value)) { isset($this->attributes['cover']) ? $this->deleteFile($this->attributes['cover'], static::IMAGEPATH) : ''; $this->attributes['cover'] = $this->uploadAllTypes($value, static::IMAGEPATH); } } // package helpers public function isSubscribedPackage($package_id): bool { return auth()->user()->subscriptions()->where('package_id', $package_id) ->whereDate('start_date', '<=', now()->format('Y-m-d')) ->whereDate('end_date', '>=', now()->format('Y-m-d')) ->where('is_active', true) ->exists() ?? false; } public function hasSubscription() { return $this->subscriptions() ->where('is_active', true) ->where('start_date', '<=', now()->format('Y-m-d')) ->where('end_date', '>=', now()->format('Y-m-d')) ->exists() ?? false; } public function hasUsedFreeTrial() { return $this->subscriptions() ->where('type', SubscriptionType::FREE_TRIAL) ->exists(); } // package helpers public function isLikedBefore($liked_user_id) { return $this->likes() ->where('liked_user_id', $liked_user_id) ->exists() ?? false; } public function isCompleted() { $requiredFields = ['country_id', 'city_id', 'gender', 'first_name', 'last_name']; return collect($requiredFields)->every(fn($field) => !is_null($this->$field)); } public function isInterestsCompleted(): bool { return $this->interestOptions()->exists() && $this->bio; } public function isFriend($id): bool { return $this->sentFriendRequests()->where('friend_id', $id)->accepted()->exists() || $this->receivedFriendRequests()->where('user_id', $id)->accepted()->exists(); } /** * Get interests grouped by their main interest name. * * @return \Illuminate\Support\Collection */ public function groupedInterests() { return $this->interestOptions->groupBy(function ($interestOption) { return $interestOption->interest->name; })->map(function ($options, $name) { $firstOption = $options->first(); return [ 'id' => $firstOption->interest->id, 'name' => $name, 'options' => $options->map(function ($option) { return [ 'id' => $option->id, 'name' => $option->name, ]; }), ]; })->values(); } public function isLiked() { if (!auth()->check()) return false; return auth()->user()->likes()->where('liked_user_id', $this->id)->exists(); } public static function boot(): void { parent::boot(); /* creating, created, updating, updated, deleting, deleted, forceDeleted, restored */ // } }
Back to File Manager