skip to Main Content

I have a web store application where from the home page you can click an input field, press find and it should show you the list of orders that are there based on the search query. When I press find it gives me a 404 not found error. Here is the home page find

                <form action="{{ route('order.search') }}" method="GET">
                <input type="text" name="search" class="search-input" placeholder="Search Orders" required>
                <button type="submit" class="search-button">Find</button>
                </form>

                </div>

here is the orderController search function

public function search(Request $request)
    {
        // echo "Search method invoked"; 

        $searchQuery = $request->input('search');
        $orders = Order::where('name', 'like', '%' . $searchQuery . '%')->get();
        return view('order.search_results', compact('searchQuery', 'orders'));
    }

here is the web.php route

Route::get('/order/search', [OrderController::class, 'search'])->name('order.search');

Here is the order page you are supposed to get to after you search for an order

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Search Results for "{{ $searchQuery }}"</div>

                <div class="card-body">
                    @if($orders->isEmpty())
                        <p>No matching orders found.</p>
                    @else
                        <ul>
                            @foreach($orders as $order)
                                <li>{{ $order->id }}</li>
                                <li>{{ $order->name }}</li>
                            @endforeach
                        </ul>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

when I search for say a Pizza it leads me to a URL of /order/search?search=Pizza, perhaps something is wrong with the route but in the orderController I believe the function takes care of that.

2

Answers


  1. Your logic is wrong, you’re making a route for a GET request but then you try to access the input which should be done with POST. Laravel is adding your search input as a url parameter.

    Either change to POST like this:

    Route::post('/order/search', [OrderController::class, 'search'])->name('order.search');
    

    Or add the parameter to your route

    Route::post('/order/search/{query}', [OrderController::class, 'search'])->name('order.search');
    
    Login or Signup to reply.
  2. Try use ->query() instead of $request->input().

    While the input method retrieves values from the entire request payload (including the query string), the query method will only retrieve values from the query string.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search