0

I’m running into a routing issue in my Laravel application and could use some help figuring it out.

In one of my views, I have a modal that allows users to search for coupons via a small Alpine.js component. To handle the search, I created a dedicated route and controller method:

Route::prefix('admin')->name('admin.')->group(function () {
    Route::get('coupons/search', [CouponController::class, 'search'])->name('coupons.search');
    Route::resource('coupons', CouponController::class)->names('coupons');
});

My assumption was that the /admin/coupons/search route would be accessible and handled correctly, but when I make the request from JavaScript, I get a 404 error.

Here’s the JavaScript involved (Alpine.js):

function couponSearch() {
    return {
        search: '',
        coupons: [],
        selectedCoupon: null,

        async searchCoupons() {
            this.selectedCoupon = null;
            if (this.search.length < 2) {
                this.coupons = [];
                return;
            }

            try {
                console.log('Searching for coupon:', this.search);

                const response = await fetch(`/admin/coupons/search?q=${encodeURIComponent(this.search)}`, {
                    method: 'GET',
                    headers: {
                        'Accept': 'application/json',
                        'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content
                    }
                });

                if (!response.ok) {
                    throw new Error(`Search failed with status: ${response.status}`);
                }

                this.coupons = await response.json();
                console.log('Coupons found:', this.coupons);
            } catch (e) {
                console.error('Error searching coupons:', e);
                this.coupons = [];
            }
        },

        selectCoupon(coupon) {
            this.selectedCoupon = coupon;
            this.search = coupon.code;
            this.coupons = [];
        }
    };
}

Here’s the controller method handling the request:

public function search(Request $request)
{
    dd($request->all()); // Debugging request

    $query = $request->input('q');

    Log::debug('Coupon search request', ['query' => $query]);

    if (empty($query)) {
        return response()->json([]);
    }

    $coupons = Coupon::where('is_active', true)
        ->where(function ($q) use ($query) {
            $q->where('code', 'like', "%$query%");
        })
        ->where(function ($q) {
            $q->whereNull('valid_until')
              ->orWhere('valid_until', '>=', now());
        })
        ->where(function ($q) {
            $q->whereNull('max_uses')
              ->orWhereRaw('uses < max_uses');
        })
        ->get(['id', 'code', 'discount_type', 'discount_value']);

    Log::debug('Coupon search results', ['count' => $coupons->count()]);

    return response()->json($coupons);
}

Console output:

Searching for coupon: test  
GET http://127.0.0.1:8000/admin/coupons/search?q=test 404 (Not Found)  
Error searching coupons: Error: Search failed with status: 404

Things I've checked:

  • The search route is defined before the Route::resource call.
  • Visiting /admin/coupons/search manually in the browser also returns a 404.
  • The routes exist in the list
GET|HEAD        admin/coupons ...... admin.coupons.index › Admin\CouponController@index  
  POST            admin/coupons ...... admin.coupons.store › Admin\CouponController@store  
  GET|HEAD        admin/coupons/create admin.coupons.create › Admin\CouponController@create  
  GET|HEAD        admin/coupons/search  admin.coupons.search › Admin\CouponController@search  
  GET|HEAD        admin/coupons/{coupon}  admin.coupons.show › Admin\CouponController@show  
  PUT|PATCH       admin/coupons/{coupon}  admin.coupons.update › Admin\CouponController@update  
  DELETE          admin/coupons/{coupon}  admin.coupons.destroy › Admin\CouponController@destroy  
  GET|HEAD        admin/coupons/{coupon}/edit  admin.coupons.edit › Admin\CouponController@edit 

Is Laravel perhaps catching the request with the Route::resource definition (admin/coupons/{coupon})? Or is there something I'm overlooking with how routes are prioritized or resolved?

11
  • what middleware is there? If a admin route, should you be sending some sort of auth? Commented May 7 at 8:43
  • are your routes cached perhaps? Commented May 7 at 9:05
  • 1
    Everything looks to be correctly defined, it has to be cache (you already said it you are not caching the routes, but just in case php artisan route:clear), and then I think it is a middleware, can you share the list of middleware being run? Commented May 7 at 9:49
  • 2
    I did another clear of the cache. Restarted my laptop and now it all works. This is by for the weirdest thing that ever happend. Is there anyway to disable the caching while in development? Commented May 7 at 10:13
  • 1
    "Is there anyway to disable the caching while in development?" - Caching is not automatically enabled in Laravel; unless you run one of the cache commands (route:cache, config:cache, view:cache, etc.), then none of this should be cached during development. And the purpose of this comment is information only; this is not trying to point fingers or start anything; I'm glad you got this working 🙂 Commented May 7 at 17:59

1 Answer 1

-2

This give you a reject becouse when laravel render the routes, know like this ex:

  1. ..admin/coupons/1 (for edit)
  2. ..admin/coupons/search (for search)

It's when you need to go to search the edit it's hide overwrite to search.

But this when search pass in URL app not get it.

You need to replace the add it after the Route::resource. Like :

Route::prefix('admin')->name('admin.')->group(function () {
    Route::resource('coupons', CouponController::class)->names('coupons');
    Route::get('coupons/search', [CouponController::class, 'search'])->name('coupons.search');
});

Or

You can delete 's' from coupons.

Route::get('coupon/search', [CouponController::class, 'search'])->name('coupons.search');
Sign up to request clarification or add additional context in comments.

1 Comment

That is not right at all, same as the other answer below, if you put the resource first and then get, it will not work at all, the current order on the question is right, try it yourself...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.