diff --git a/app/Http/Controllers/SearchController.php b/app/Http/Controllers/SearchController.php new file mode 100644 index 0000000..3fdbe80 --- /dev/null +++ b/app/Http/Controllers/SearchController.php @@ -0,0 +1,96 @@ +get('q', '')); + + if ($query === '') { + // No query → send the user back to the browse feed instead of an + // empty "no results" page. + return redirect()->route('videos.index'); + } + + $like = '%' . $query . '%'; + $qLower = mb_strtolower($query); + + // ── Videos ──────────────────────────────────────────────── + // Match on text columns AND on any audio-track language that aligns + // with the query (e.g. searching "arabic" surfaces videos that carry + // an Arabic track even if the primary language is English). + $trackVideoIds = VideoAudioTrack::whereRaw('LOWER(language) = ?', [$qLower]) + ->pluck('video_id'); + + $videoHits = Video::public() + ->where(function ($q) use ($like, $qLower, $trackVideoIds) { + $q->where('title', 'like', $like) + ->orWhere('description','like', $like) + ->orWhere('tags', 'like', $like) + ->orWhereRaw('LOWER(language) = ?', [$qLower]) + ->orWhereIn('id', $trackVideoIds); + }) + ->latest() + ->limit(60) + ->get(); + + $videos = $videoHits->where('is_shorts', false)->values(); + $shorts = $videoHits->where('is_shorts', true)->values(); + + // ── Channels ────────────────────────────────────────────── + $channels = User::where(function ($q) use ($like) { + $q->where('name', 'like', $like) + ->orWhere('username','like', $like) + ->orWhere('bio', 'like', $like); + }) + ->orderByDesc('id') + ->limit(12) + ->get(); + + // ── Playlists ───────────────────────────────────────────── + $playlists = Playlist::where('visibility', 'public') + ->where(function ($q) use ($like) { + $q->where('name', 'like', $like) + ->orWhere('description','like', $like); + }) + ->withCount('videos') + ->with(['user', 'videos' => fn ($q) => $q->orderBy('playlist_videos.position')->limit(1)]) + ->latest() + ->limit(12) + ->get(); + + return view('videos.index', [ + 'videos' => $videos, + 'shorts' => $shorts, + 'playlists' => $playlists, + 'channels' => $channels, + 'query' => $query, + 'langChips' => [], + ]); + } +} diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index 30a1186..4491d28 100644 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -1237,10 +1237,11 @@
diff --git a/resources/views/layouts/partials/header.blade.php b/resources/views/layouts/partials/header.blade.php index 2b08655..4c89af4 100644 --- a/resources/views/layouts/partials/header.blade.php +++ b/resources/views/layouts/partials/header.blade.php @@ -15,9 +15,10 @@@if($videos->count()) {{ $videos->count() }} video{{ $videos->count() !== 1 ? 's' : '' }} @endif - @if($searchShorts->count()) · {{ $searchShorts->count() }} short{{ $searchShorts->count() !== 1 ? 's' : '' }} @endif + @if($searchShorts->count()) · {{ $searchShorts->count() }} short{{ $searchShorts->count() !== 1 ? 's' : '' }} @endif + @if($searchChannels->count()) · {{ $searchChannels->count() }} channel{{ $searchChannels->count() !== 1 ? 's' : '' }} @endif @if($searchPlaylists->count()) · {{ $searchPlaylists->count() }} playlist{{ $searchPlaylists->count() !== 1 ? 's' : '' }} @endif @if($totalResults === 0) No results found @endif