elo
195
6
Laravel

Sending Notification on Successful events

Posted 1 month ago by elo

I am working on an Laravel 5.8 API application that need to notify users when they successfully place an order. The order process works but I am not getting the email notification. Here's how I am going about it using mailtrap for demo purpose

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=**************
MAIL_PASSWORD=**************
MAIL_ENCRYPTION=tls

In Order model I am using the notifiable trait

<?php

namespace App;

use App\User;
use App\Product;
use Illuminate\Notifications\Notifiable;

use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
    use Notifiable;

    ...
}

Created the OrderSuccesful notification and using it in the OrderController store method like so

<?php

...

use App\Notifications\OrderSuccessful;

use App\Order;
use Illuminate\Http\Request;

class OrderController extends Controller
{
    public function store(Request $request, Product $product)
    {
        // create order
        $order = Order::create([
            'user_id'       => auth()->user()->id,
            'product_id'    => $product->id,
            'barrels'       => $request->barrels,
            'status'        => 'successful'
        ]);

        $user = auth()->user();
        
        // notify user of successful order
        $user->notify(new OrderSuccessful($order));

        return new OrderResource($order);
    }
}

Now when I make a successful order, I don't get a notification. Since I am using the notification for the first time, I am wondering what I have missed. Appreciate it if someone can point that out and tell me how to fix it.

Please sign in or create an account to participate in this conversation.