Laravel 11 E-Commerce - Admin Update Order Status

Welcome back to the Laravel E-Commerce Project Tutorial. In this tutorial, we are going to learn about updating the order status.

Step 1: Create a Function in AdminController

First, let's create a function in the AdminController to update the order status:


public function update_order_status(Request $request){
$order = Order::find($request->order_id);
$order->status = $request->order_status;
if($request->order_status=='delivered')
{
$order->delivered_date = Carbon::now();
}
else if($request->order_status=='canceled')
{
$order->canceled_date = Carbon::now();
}
$order->save();
if($request->order_status=='delivered')
{
$transaction = Transaction::where('order_id',$request->order_id)->first();
$transaction->status = "approved";
$transaction->save();
}
return back()->with("status", "Status changed successfully!");
}

Step 2: Create a Route for the Function

Now, let's create a route for this function:


Route::put('/admin/order/update-status',[AdminController::class,'update_order_status'])->name('admin.order.status.update');

Step 3: Add a Form to the Order Details Page

Next, let's go to the order-details.blade.php file and add a form:


<div class="wg-box mt-5">
<h5>Update Order Status</h5>
<form action="{{route('admin.order.status.update')}}" method="POST">
@csrf
@method("PUT")
<input type="hidden" name="order_id" value="{{ $transaction->order->id }}" />
<div class="row">
<div class="col-md-3">
<div class="select">
<select id="order_status" name="order_status">
<option value="ordered" {{$transaction->order->status=="ordered" ? "selected":""}}>Ordered</option>
<option value="delivered" {{$transaction->order->status=="delivered" ? "selected":""}}>Delivered</option>
<option value="canceled" {{$transaction->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</button>
</div>
</div>
</form>
</div>

Step 4: Display the Confirmation Message

Now, let's add a confirmation message after the back button div:


<div class="flex items-center justify-between gap10 flex-wrap">
<div class="wg-filter flex-grow">
<h5>Ordered Details</h5>
</div>
<a class="tf-button style-1 w208" href="{{route('admin.orders')}}">Back</a>
</div>
@if(Session::has('status'))
<p class="alert alert-success">{{Session::get('status')}}</p>
@endif

Testing the Order Status Update

Now it's done! Let's check this. Go to the orders page, click on the link for the order details, and you can see the form. Now, let's change the status. Click on the dropdown and click on update, and you can see the order status has been changed.

If you select the order status as delivered, the order status will be updated to "order delivered" and the delivery date will be displayed. If you select the order status as cancelled, the order status will be updated to "order cancelled" and the cancellation date will be displayed.

In this way, you can update the order status.

That's all about updating order status.