In this lesson, we will email the restaurant owner when the new order is placed.
An email will contain all the data to process an order. The workflow is pretty straightforward:
First, let's create a NewOrderCreated
Notification. We can add the --markdown
flag to note that the template will contain a markdown, and the value of that flag is the template's location.
php artisan make:notification NewOrderCreated --markdown=mail.order.new-order-created
Notification will accept an $order
parameter and then load restaurant
, products
, and customer
relationships in the __construct()
method.
app/Notifications/NewOrderCreated.php
namespace App\Notifications; use App\Models\Order;use App\Models\Restaurant;use App\Models\User;use Illuminate\Bus\Queueable;use Illuminate\Contracts\Queue\ShouldQueue;use Illuminate\Database\Eloquent\Collection;use Illuminate\Notifications\Messages\MailMessage;use Illuminate\Notifications\Notification; class NewOrderCreated extends Notification{ use Queueable; protected Order $order; protected Restaurant $restaurant; protected Collection $products; protected User $customer; /** * Create a new notification instance. */ public function __construct(Order $order) { $this->order = $order; $this->restaurant = $order->restaurant; $this->products = $order->products; $this->customer = $order->customer; } /** * Get the notification's delivery channels. * * @return array<int, string> */ public function via(object $notifiable): array { return ['mail']; } /** * Get the mail representation of the notification. */ public function toMail(object $notifiable): MailMessage { return (new MailMessage) ->subject(__('[:restaurant_name] New Order', [ 'restaurant_name' => $this->restaurant->name, ])) ->markdown('mail.order.new-order-created', [ 'order' => $this->order, 'restaurant' => $this->restaurant, 'products' => $this->products, 'customer' => $this->customer, ]); } /** * Get the array representation of the notification. * * @return array<string, mixed> */ public function toArray(object $notifiable): array { return [ // ]; }}
We pass the data to the template via the ->markdown()
method like you would do with the blade file via the view()
method.
Now let's update the new-order-created.blade.php
template with the following content.
resources/views/mail/order/new-order-created.blade.php
<x-mail::message># Order #{{ $order->id }} # {{ $restaurant->name }} ## Customer {{ $customer->name }}[{{ $customer->email }}](mailto:{{ $customer->email }}) ## Order Items @foreach($products as $product)- {{ $product->name }} {{ number_format($product->price / 100, 2) }} €@endforeach ## Total {{ (number_format($order->total / 100, 2)) }} € Thanks,<br>{{ config('app.name') }}</x-mail::message>
Finally, update the OrderController
and fire NewOrderCreated
Notification.
app/Http/Controllers/Customer/OrderController.php
$user = $request->user();$attributes = $request->validated(); DB::transaction(function () use ($user, $attributes) { $order = DB::transaction(function () use ($user, $attributes) { $order = $user->orders()->create([ 'restaurant_id' => $attributes['restaurant_id'], 'total' => $attributes['total'], 'status' => OrderStatus::PENDING, ]); $order->products()->createMany($attributes['items']); return $order;}); $order->restaurant->owner->notify(new NewOrderCreated($order)); session()->forget('cart'); return to_route('customer.orders.index')
This is it, and now the owner will get notified when a new order is made.