Laravel 8 E-Commerce - Admin Update Order Status
In this video, we will learn about updating the order status from the admin panel.
Specifically, we will explore how to update the order status in the admin panel.
First, let's add some columns to the orders table.
To do this, we need to create a migration.
Switch to the command prompt and run the following command:
php artisan make:migration add_delivered_canceled_date_to_orders_table --table=orders
Now, let's open the migration file and add the following code:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddDeliveredCanceledDateToOrdersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('orders', function (Blueprint $table) {
$table->date('delivered_date')->nullable();
$table->date('canceled_date')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('orders', function (Blueprint $table) {
$table->dropColumn('delivered_date','canceled_date');
});
}
}
Next, let's migrate this migration.
Switch to the command prompt and run the following command:
php artisan migrate
Now, let's run the application:
php artisan serve
Next, switch to the project and open the admin-order-component.blade.php
view file.
Add a dropdown inside the table body:
<td>
<div class="dropdown">
<button class="btn btn-success btn-sm dropdown-toggle" type="button" data-toggle="dropdown">Status
<span class="caret"></span></button>
<ul class="dropdown-menu">
<li><a href="#" wire:click.prevent="updateOrderStatus({{$order->id}},'delivered')">Delivered</a></li>
<li><a href="#" wire:click.prevent="updateOrderStatus({{$order->id}},'canceled')">Canceled</a></li>
</ul>
</div>
</td>
Add the following code to the table header:
Now, go to the AdminOrderComponent.php
class file.
Create a function to update the order status:
public function updateOrderStatus($order_id,$status)
{
$order = Order::find($order_id);
$order->status = $status;
if($status == "delivered")
{
$order->delivered_date = DB::raw('CURRENT_DATE');
}
else if($status == "canceled")
{
$order->canceled_date = DB::raw('CURRENT_DATE');
}
$order->save();
session()->flash('order_message','Order status has been updated successfully!');
}
Import the DB facade:
use Illuminate\Support\Facades\DB;
Now, let's test it out.
Switch to your browser and refresh the page.
You should see a dropdown menu.
Change the status by selecting an option from the dropdown.
For example, select "Delivered" and click to update.
You should see the updated status and the delivered date.
Try changing the status of another order to "Canceled".
Update the status and check the details to see the canceled date.
And that's how you can update the order status in the admin panel.