URL error on page displaying search results

Issue

This Content is from Stack Overflow. Question asked by scottie320

I want to add a store information search field to the top page of a site created with laravel and display the search results on a separate page.
Currently, pressing the “Search” button results in a 404 error.
The URL at that time is “example.com/shop/search?keyword=&“category”=”.
Nothing in error.log. where is the cause?

Code
php artisan route:list

| GET|HEAD  | shop/search   | shop.search                  | AppHttpControllersShopController@search                                  | web 

home.blade.php

    <div class=“c-form__select”>
        <form method="GET" action="{{ route('shop.search')}}">
        <input name="keyword" type="text" placeholder="Enter Keyword" value="{{ $params['keyword'] ?? null }}">
        <select name=“category” class=“c-form__control ”>
            <option selected=“selected” value=“”>Please select</option>
            @foreach($categories as $category)
            <option value={{ $category->id }}> {{ $category->name }}</option>
            @endforeach
        </select>
        <button type="submit">Search</button>
    </form>
    </div>

Search.blade.php


    <div class="l-section_bgpink l-section"><div class="l-section__inner">
        @if (!$shops->isEmpty())
        <ul class="c-shop">
            @foreach ($shops as $shop)
            <li><a href="{{ route('shop.show', ['shop' => $shop]) }}">
                <div class="logo"><img src="{{ $shop->logo ? asset('storage/'.config('constants.UPLOAD_IMG_SHOP').'/'.$shop->logo) : asset('/assets/img/shop/img_noimage.jpg') }}" alt="{{ $shop->name }}"></div>
                <div class="name">{{ $shop->name }}</div>
            </a></li>
            @endforeach
        </ul>
        @else
        <p class="c-txt">Your store is not registered.</p>
        @endif
</div></div>

ShopController.php

    public function search(Request $request)
    {
        $params = $request->query();

        $shops= Shop::scopeSearch($params)->get();

        $slides = Slide::where([
            ['area_id', '=', config('constants.AREA_ID')],
            ['status', '=', 1],
        ])->orderBy('created_at')->get();

        return view('shop.search', [
            'shops' => $shops,
            'slides' => $slides,
        ]);
    }

App/Models/Shop.php

/**
 * @param IlluminateDatabaseEloquentBuilder
 * @param array
 * @return IlluminateDatabaseEloquentBuilder
 */
public function scopeSearch(Builder $query, array $params): Builder
{

    if (!empty($params['category_id'])){
        $query
        ->where(['area_id', '=', config('constants.AREA_ID')],
                ['category_id', '=', $id],
                ['status', '=', '1']);
    }

    // keywords
    if (!empty($params['keyword'])) {
        $query->where(function ($query) use ($params) {
            $query->where('name', 'like', '%' . $params['keyword'] . '%')
                ->orWhere('region', 'like', '%' . $params['keyword'] . '%')
                ->orWhere('back', 'like', '%' . $params['keyword'] . '%')
                ->orWhere('salary_assessment', 'like', '%' . $params['keyword'] . '%')
                ->orWhere('memo', 'like', '%' . $params['keyword'] . '%')
                ->orWhere('greeting', 'like', '%' . $params['keyword'] . '%');
        });
    }

    return $query;
}

web.php

    Route::get('shop/search', 'ShopController@search')->name('shop.search');



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?