【问题标题】:How to Update Order Status by User in Laravel 7如何在 Laravel 7 中按用户更新订单状态
【发布时间】:2020-10-27 17:02:40
【问题描述】:

我正在制作一个电子商务网站,并尝试为普通用户/客户完成我的订单管理。如果状态仍处于待处理状态,我想允许客户取消订单。我有一个OrderHistoryController,内容如下:

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');
}

我在 web.php 中创建了一条路线:

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

我的刀片文件有一个按钮:

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

我想要做的是在单击按钮后将我的Order 表中的状态从“待处理”更新为“已取消”。当我这样做时,我在页面 localhost:8000/order-history/cancel 上被重定向

404|Not Found

似乎是什么问题?或者有没有其他方法可以做到这一点?任何建议将不胜感激。提前致谢!

【问题讨论】:

    标签: laravel e-commerce crud


    【解决方案1】:

    您的路线中缺少订单 ID,因此请更改:

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

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

    【讨论】:

      猜你喜欢
      • 2021-05-06
      • 2017-07-18
      • 1970-01-01
      • 1970-01-01
      • 2019-08-27
      • 1970-01-01
      • 1970-01-01
      • 2014-09-06
      • 2017-07-16
      相关资源
      最近更新 更多