dimanche 26 janvier 2020

Why isn't my condition working in search?

I am trying to get a searched result and I have the following code:

public function search(Request $request){ 
    $from = $request->from;
    $to = $request->to;
    $word = $request->word;  
    if(empty($word) || $word == null){
        $searched = Post::whereBetween('created_at', [$from, $to])->paginate(10);
    } elseif(!empty($word) && !empty($from) && !empty($to)){ 
        $searched = Post::where('title', 'LIKE', '%' . $word . '%')
        ->orWhere('content', 'LIKE', '%' . $word . '%')
        ->orWhere('subtitle', 'LIKE', '%' . $word . '%')
        ->whereBetween('created_at', [$from, $to])
        ->paginate(10); 
    } elseif(empty($from) && empty($to) && !empty($word)){
        $searched = Post::where('title', 'LIKE', '%' . $word . '%')
        ->orWhere('content', 'LIKE', '%' . $word . '%')
        ->orWhere('subtitle', 'LIKE', '%' . $word . '%')
        ->paginate(10);
    }

    return view('page.search', compact('searched', 'from', 'to'));
}

The first and second condition are working but not the last. What am I missing or doing wrong here?

Aucun commentaire:

Enregistrer un commentaire