4 years ago

How Can I Improve This Code

Posted 4 years ago by shammadahmed

How can i improve this code: It doesn't work when there is no search term, the page simply reloads.

public function search(Request $request)
    $q = $request->get('q');

    if ($request->has('q')) {

        if (empty($q)) {
            return redirect('search')->with('status', 'Please, insert a search term.');
        } else {
            $shirts = Shirt::where('name', 'LIKE', '%'.$q.'%')->get();

            if (empty($shirts->toArray())) {
                return redirect('search')->with('status', 'No products were found matching that search term.')->withInput();

        return view('search', compact('shirts'));

    return view('search');

Please sign in or create an account to participate in this conversation.