0

I have a very simple web app created in Laravel 5.5:

There is a database with a list of coupon codes that have either not been redeemed or been redeemed. 0 is not redeemed and 1 is redeemed.

When someone enters a string into a HTML form input and submits it, Laravel goes to a route with that string as a variable.

The Controller code is as follows:

public function redeemCoupon ($coupon_code)

    {
        $coupon = Coupon::where('coupon_code', $coupon_code)->first();

        if ($coupon === null) {
            return view ('pages.no-coupon');
        }

        else if ($coupon->redeemed == 1) {
            return view ('pages.used-coupon');  
        }
        else {

            $coupon->redeemed = 1;
            $coupon->update();
            return view('pages.redeemed-coupon', compact('coupon') );
        }
    }

Route:

Route::post('/redeem-coupon/{coupon_code}', 'CouponController@redeemCoupon');

You can try it out here: http://178.62.4.225

Everything works fine when done normally, tested on the code "code01". When I enter it and it hasn't been redeemed, it says so, and redeeming it changes the column in the database from 0 to 1. If I try the process again it tells me it has already been redeemed.

The issue is when I'm on the page that tells me it's been redeemed: http://178.62.4.225/redeem-coupon/code01

If I refresh it with CTRL + R, it just reloads and says it's already been redeemed. But if I paste the URL into a new tab or click into it and refresh by clicking enter, it gives " MethodNotAllowedHttpException" and the resulting debug screen, from what I can tell, offers nothing of use.

Help!

3 Answers 3

1

Changing

Route::post('/redeem-coupon/{coupon_code}', 'CouponController@redeemCoupon');

to

Route::any('/redeem-coupon/{coupon_code}', 'CouponController@redeemCoupon');

Did the trick

Sign up to request clarification or add additional context in comments.

2 Comments

Why any and not get method?
GET gives the same error I was using as POST. I also need it to do both; if "redeemed" in the DB = 0 I need POST to change it to 1, if it already = 1 I need it to return the view that says so with GET.
0

You are doing a GET request and define a post route change

Route::post('/redeem-coupon/{coupon_code}', 'CouponController@redeemCoupon');

to:

Route::get('/redeem-coupon/{coupon_code}', 'CouponController@redeemCoupon');

2 Comments

This still gives the same error. But I don't see how that would work as it needs to be a post request for when the code hasn't been redeemed, in order to change the DB column from a 0 to a 1?
@RuairiMcNicholas So you should load the page and then check the coupon status with post request via AJAX
0

Is redeemed set as protected? Also displaying app_debug true displays all your DB connection info (user and pass)

More than likely to be due to the _method.

What page is _method = "POST" on?

2 Comments

In the Coupon model I have it listed under "protected $fillable = [" which I think means its protected, should it not be?
@RuairiMcNicholas Thats just for when the model is updated. It is probably to do with the _method assigned to it. Each _method associates to a route type. laravel.com/docs/5.5/routing

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.