Laravel 12 E-Commerce Project Tutorial
Admin: Updating the Order Status
Hi everyone, welcome back to the Laravel E-Commerce Project Tutorial. This video focuses on implementing the functionality for the Admin to update an order'sstatus (e.g., from "ordered" to "delivered" or "canceled").
-
Create the `update_order_status` Function in `AdminController`
Open your `AdminController` and create the `update_order_status` function. This function handles the form submission, updates the `Order` status, sets the corresponding date (`delivered_date` or `canceled_date`), and updates the `Transaction` status if the order is marked as "delivered".
use CarbonCarbon; // Make sure to include Carbon
public function update_order_status(Request $request){
// 1. Find the Order
$order = Order::find($request->order_id);
$order->status = $request->order_status;
// 2. Update status-specific dates
if($request->order_status == "delivered")
{
$order->delivered_date = Carbon::now();
$order->canceled_date = null; // Clear canceled date if delivered
}
else if($request->order_status == "canceled")
{
$order->canceled_date = Carbon::now();
$order->delivered_date = null; // Clear delivered date if canceled
}
else // Handle status returning to "ordered" or "pending" if necessary
{
$order->delivered_date = null;
$order->canceled_date = null;
}
$order->save();
// 3. Update Transaction Status (Only if delivered)
if($request->order_status == "delivered")
{
$transaction = Transaction::where("order_id", $request->order_id)->first();
// Assuming COD and manual approval: Delivered orders imply payment is approved.
$transaction->status = "approved";
$transaction->save();
}
return back()->with("status", "Status changed successfully!");
} -
Define the Route for Updating Status
Define the route in `web.php` for the update action. We use the PUT HTTP method for updates.
Route::put("/admin/order/update-status",[AdminController::class,"update_order_status"])->name("admin.order.status.update"); -
Add the Status Update Form to `order-details.blade.php`
Open the `order-details.blade.php` view (created in Part 41) and integrate the form for updating the status, usually near the top of the order information section.
<div class="card p-4 mb-4">
<h5>Update Order Status</h5>
@if (Session::has("status"))
<p class="alert alert-success">{{ Session::get("status") }}</p>
@endif
<form action="{{ route("admin.order.status.update") }}" method="POST">
@csrf
@method("PUT")
<input type="hidden" name="order_id" value="{{ $order->id }}" />
<div class="row align-items-center">
<div class="col-md-4">
<div class="single-form">
<select class="form-control" name="order_status" required>
<option value="ordered" {{ $order->status == "ordered" ? "selected" : "" }}>Ordered</option>
<option value="delivered" {{ $order->status == "delivered" ? "selected" : "" }}>Delivered</option>
<option value="canceled" {{ $order->status == "canceled" ? "selected" : "" }}>Canceled</option>
</select>
</div>
</div>
<div class="col-md-3">
<button type="submit" class="btn btn-primary tf-button w208">Update Status</button>
</div>
</div>
</form>
</div>
Verification and Testing 🔄
1. Navigate to an existing order'sdetails page in the admin panel (`/admin/order/details/{order_id}`).
2. Use the dropdown to select a new status (e.g., Delivered) and click Update Status.
3. Verify that the page reloads with a success message, the order status badge changes, and the transaction status is updated to Approved if "Delivered" was selected. If "Canceled" was selected, the order status should change to Canceled.
