如何在搜索框上过滤 Laravel 8 中的布尔值

How to filter on a search box a boolean in Laravel 8

我想要在前端显示为 'Approved' 和 'Not Approved' 的布尔字段,我可以将其过滤为在前端显示吗?

我的过滤逻辑

$search = '%'.$this->search . '%';
        $comments = Comment
        ::where('title', 'like', $search)
       ->orWhere('name', 'like', $search)
       ->orWhere('email', 'like', $search)
       ->orWhere('message', 'like', $search)
       ->orWhere('approved', 'like', $search)
       ->orderBy('created_at', 'desc')       
       ->paginate($this->paginate);

我只能将批准的列搜索为 0 和 1。

试试下面的查询 -

$search = '%'.$this->search . '%';
$searchInput = $this->search;

$comments = Comment::where('title', 'like', $search)->where(function ($q) use ($searchInput, $search) {
    $q->orWhere('name', 'like', $search);
    $q->orWhere('email', 'like', $search);
    $q->orWhere('message', 'like', $search);
    $q->orWhereRaw("(CASE WHEN approved = 1 THEN 'Approved' ELSE 'Not Approved' END) LIKE '%$searchInput%'");
})->orderBy('created_at', 'desc')->paginate($this->paginate);