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 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; } // 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() ?? false; } // package helpers public function isLikedBefore($liked_user_id) { return $this->likes() ->where('liked_user_id', $liked_user_id) ->exists() ?? false; } public function isCompleted() { $requiredFields = ['birth_date', '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)->exists() || $this->receivedFriendRequests()->where('user_id', $id)->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(); } }
Back to File Manager