belongsTo(User::class, 'user_id'); } public function category(): BelongsTo { return $this->belongsTo(DealCategory::class, 'deal_category_id'); } public function interactions(): HasMany { return $this->hasMany(Interaction::class); } public function reports(): BelongsToMany { return $this->belongsToMany(Report::class); } /** * Scope a query to only include active deals */ #[Scope] public function WithActiveDeals(Builder $query): Builder { return $query->where('active', true); } /** * Scope a query to determine if the current user has liked and favorite a deal */ #[Scope] public function WithCurrentUserInteractions(Builder $query): Builder { return $query->withExists([ 'interactions as is_liked' => function ($query) { $query->where('user_id', Auth::id()) ->where('type', InteractionType::Like); }, 'interactions as is_favorite' => function ($query) { $query->where('user_id', Auth::id()) ->where('type', InteractionType::Favorite); }, ]); } /** * Scope a query to get total like count per deal */ #[Scope] public function WithLikePerDeal(Builder $query): Builder { return $query->withCount([ 'interactions as total_likes' => function ($query) { $query->where('type', InteractionType::Like); }, ]); } /** * Scope a query to get click count per deal */ #[Scope] public function WithRedirectionPerDeal(Builder $query): Builder { return $query->withSum([ 'interactions as total_redirection' => function ($query) { $query->where('type', InteractionType::Redirection); }, ], 'count'); } /** * Scope a search in a query */ #[Scope] public function search(Builder $query, string $search): Builder { return $query ->where('title', 'LIKE', "%$search%") ->orWhereRelation('broker', 'name', 'LIKE', "%$search%"); } /** * Scope a category filter in a query */ #[Scope] public function filterByCategory(Builder $query, string $category): Builder { return $query->where('deal_category_id', $category); } }