<?php

namespace App\Http\Controllers;

use App\Models\VideoComments;
use Illuminate\Routing\Controller as BaseController;

class CommentQueryController extends BaseController
{
    public function index() {
        $keyword = request()->get("keyword", "");
        $query = VideoComments::query()->where("is_top", "=", 1)->with("video")->orderByDesc("created_at");
        if ($keyword) {
            $keyword_split = explode(" ", $keyword);
            foreach ($keyword_split as $_keyword) {
                if (mb_strlen(trim($_keyword)) > 0) {
                    $query->where("content", "like", "%{$_keyword}%");
                }
            }
        }
        $comments = $query->limit(20)->get();
        return view("index", [
            "keyword" => $keyword,
            "comments" => $comments,
        ]);
    }
}