Search code examples
laravele-commercecrud

How to Update Order Status by User in Laravel 7


I am making an e-commerce website and trying to finish my order management for normal users/customers. I want to allow the customer to cancel an order if the status is still pending. I have an OrderHistoryController with the following:

public function cancel(Order $order)
{
    $order->status = 'canceled';
    $order->save();

    //check if all suborders are canceled
    $pendingSubOrders = $order->subOrders()->where('status','!=', 'canceled')->count();

    if($pendingSubOrders == 0) {
        $order->order()->update(['status'=>'canceled']);
    }

    return redirect('/order-history/{order}')->withMessage('Order was canceled');
}

I made a route in my web.php:

Route::get('/order-history/cancel', 'OrderHistoryController@cancel')->name('order-history.cancel')->middleware('auth');

and my blade file has a button:

 @if ($order->status == 'pending')
 <button type="submit" class="default-btn floatright"><a href="{{route('order-history.cancel', $order)}}"> Cancel Order</a></button>
 @endif

What I want to do is update the status in my Order table from "pending" to "canceled" once button was clicked. When I do this, I get redirected on the page localhost:8000/order-history/cancel

404|Not Found

What seems to be the problem? Or is there any other way I can do this? Any advice would be much appreciated. Thanks in advance!


Solution

  • You missing the id of order in your route, so change:

    Route::get('/order-history/cancel', 'OrderHistoryController@cancel')->name('order-history.cancel')->middleware('auth');
    

    To

    Route::get('/order-history/cancel/{order}', 'OrderHistoryController@cancel')->name('order-history.cancel')->middleware('auth');