Laravel 7 如何按用户更新订单状态
How to Update Order Status by User in Laravel 7
我正在制作一个 e-commerce 网站并尝试完成正常 users/customers 的订单管理。如果状态仍未定,我想允许客户取消订单。我有一个 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');
我的 blade 文件有一个按钮:
@if ($order->status == 'pending')
<button type="submit" class="default-btn floatright"><a href="{{route('order-history.cancel', $order)}}"> Cancel Order</a></button>
@endif
我想要做的是在单击按钮后将 Order
table 中的状态从“待定”更新为“已取消”。当我这样做时,我被重定向到页面 localhost:8000/order-history/cancel
404|Not Found
好像是什么问题?还是有其他方法可以做到这一点?任何建议将不胜感激。提前致谢!
您缺少路线中的订单 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');
我正在制作一个 e-commerce 网站并尝试完成正常 users/customers 的订单管理。如果状态仍未定,我想允许客户取消订单。我有一个 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');
我的 blade 文件有一个按钮:
@if ($order->status == 'pending')
<button type="submit" class="default-btn floatright"><a href="{{route('order-history.cancel', $order)}}"> Cancel Order</a></button>
@endif
我想要做的是在单击按钮后将 Order
table 中的状态从“待定”更新为“已取消”。当我这样做时,我被重定向到页面 localhost:8000/order-history/cancel
404|Not Found
好像是什么问题?还是有其他方法可以做到这一点?任何建议将不胜感激。提前致谢!
您缺少路线中的订单 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');