Skip to content

Instantly share code, notes, and snippets.

@sirawitpra
Last active July 6, 2017 14:26
Show Gist options
  • Save sirawitpra/a1057a7755ebaba4e8f9c02f6916c250 to your computer and use it in GitHub Desktop.
Save sirawitpra/a1057a7755ebaba4e8f9c02f6916c250 to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Controllers;
use App\Order;
use App\Product;
use App\Mail\NewOrder;
use App\Mail\LowStock;
use Illuminate\Support\Facades\Mail;
use App\Http\Controllers\Controller;
class OrderController extends Controller
{
public function create()
{
// create order
$order = \App\Order::create(request());
// send confirmation email
Mail::to($order->customer_email)->send(new NewOrder($order));
// update stock
foreach ($order->items as $item) {
$product = Product::find($item->productId);
$quantityLeft = $product->quantity - $item->quantity;
$product->update([
'quantity' => $quantityLeft
]);
// if stock is low
if ($quantityLeft < 3) {
// send stock is low email
Mail::to(config('app.admin_email'))->send(new LowStock($product));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment