user()->id ?? null); $page = max(1, (int) $request->query('page', 1)); $hits = 20; $start = ($page - 1) * $hits; $total = 0; $results = collect(); try { $query = ArtworkFavourite::with(['artwork.user']) ->where('user_id', $userId) ->orderByDesc('created_at') ->orderByDesc('artwork_id'); $total = (int) $query->count(); $favorites = $query->skip($start)->take($hits)->get(); $results = $favorites->map(function ($fav) { $art = $fav->artwork; if (! $art) { return null; } $item = (object) $art->toArray(); $item->uname = $art->user?->username ?? $art->user?->name ?? null; $item->datum = $fav->created_at; return $item; })->filter(); } catch (\Throwable $e) { $total = 0; $results = collect(); } $results = collect($results)->filter()->values()->transform(function ($row) { $row->name = $row->name ?? $row->title ?? ''; $row->slug = $row->slug ?? Str::slug($row->name); $row->encoded = isset($row->id) ? app(\App\Helpers\Thumb::class)::encodeId((int) $row->id) : null; return $row; }); $displayName = $username ?: (DB::table('users')->where('id', $userId)->value('username') ?? ''); $page_title = $displayName . ' Favourites'; return view('user.favourites', [ 'results' => $results, 'page_title' => $page_title, 'user_id' => $userId, 'page' => $page, 'hits' => $hits, 'total' => $total, ]); } public function destroy(Request $request, $userId, $artworkId) { $auth = $request->user(); if (! $auth || $auth->id != (int)$userId) { abort(403); } $creatorId = (int) DB::table('artworks')->where('id', (int) $artworkId)->value('user_id'); DB::table('artwork_favourites')->where('user_id', (int) $userId)->where('artwork_id', (int) $artworkId)->delete(); if ($creatorId) { app(UserStatsService::class)->decrementFavoritesReceived($creatorId); } return redirect()->route('legacy.favourites', ['id' => $userId])->with('status', 'Removed from favourites'); } }