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); } /** * Get deals that are active */ public function scopeWithActiveDeals(Builder $query): Builder { return $query->where('active', true); } /** * Get if the current user has liked or favorite the deal */ public function scopeWithCurrentUserInteractions(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); }, ]); } public function scopeWithLikes(Builder $query): Builder { return $query->withCount([ 'interactions as total_likes' => function ($query) { $query->where('type', InteractionType::Like); }, ]); } public function scopeWithRedirections(Builder $query): Builder { return $query->withSum([ 'interactions as total_redirection' => function ($query) { $query->where('type', InteractionType::Redirection); }, ], 'count'); } public function reports(): BelongsToMany { return $this->belongsToMany(Report::class); } }