Last active
July 6, 2017 14:26
-
-
Save sirawitpra/a1057a7755ebaba4e8f9c02f6916c250 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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