【发布时间】:2025-11-28 10:05:01
【问题描述】:
我正在尝试提交一个表单来更新我在数据库中的表并测试控制器是否正常工作,我做了一些测试。我正在学习如何使用 POST 方法传递数据。这是我的代码。
路线:
Route::prefix('dashboard')->group(function () {
// Route for panel.
Route::prefix('panel')->group(function () {
// Return index home page for panel.
Route::get('/', 'Panel\DashboardController@index');
// Update order information (delivery date and order status)
Route::put('/update-order-information', 'Order\OrderController@update');
});
}
panel.blade.php(视图):
<table class="table table-light ">
<thead class="thead-dark">
<tr>
<th scope="col">Order ID</th>
<th scope="col">Delivery Date</th>
<th scope="col">Order Info</th>
<th scope="col">Order Status</th>
<th scope="col">Purchase Order</th>
</tr>
</thead>
<tbody>
@foreach ($customerOrders as $customerOrder)
<tr>
<form action="/dashboard/panel/update-order-information/{{$customerOrder->order_id}}" method="POST">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<td>{{$customerOrder->order_id}}</td>
<td>
<input name='delivery_date' class="date form-control" type="text" placeholder="Select delivery date"
autocomplete="off">
</td>
<td>{{$customerOrder->product_name}}</td>
<td>
<select id="status" name="status">
<option value="inProgress">In Progress</option>
<option value="shipped">Order Shipped</option>
<option value="cancelled">Cancelled</option>
</select>
</td>
<td>{{$customerOrder->purchase_order}}
<input type="submit" value="Submit">
</td>
</form>
</tr>
@endforeach
</tbody>
</table>
OrderController.php:
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$order = new Order();
findOrFail($id);
$order->order_id = $request->input('delivery_date');
$order->order_status = $request->input('status');
$order->save();
}
当我点击提交按钮时,它给了我一个 404 页面。谁能指出我的错误?
【问题讨论】:
-
而不是
Route::put(尝试Route::post(因为您在表单上使用post 方法,我认为它应该给您,方法不允许异常而不是404 -
@AkshayKhale 我的代码逻辑正确吗?
-
使用 Dilip 在答案中建议的方法,这是最推荐的方法之一