简体   繁体   中英

Laravel/Voyager: Send email when new data is created using BREAD

I have a coupons table BREAD with Voyager whereby each coupon will have an email address. I would like to send an email to the specific email address which is associated with the coupon when a new coupon has been created.

Coupons table:

在此处输入图片说明

For example from the above picture, after I have created the coupon named: haha123, I would like to send an email to cdn@gmail.com.

As I didn't make a custom controller for generating new coupon and has been only using the default BREAD function from Voyager, hence I am unsure where and how should I do it.

SOLVED:

This is most likely not the best way as I didn't make use of the voyager events. Hence, I just do it the troublesome way by adding my own custom voyager CouponsController to overwrite the default controller and add the laravel mail function in the store method of within the custom CouponsController.

Maybe helpful, Send email when model status updated (Laravel Voyager Admin)

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Mail;

class Contact extends Model
{
    public $table = 'contacts';
    public $fillable = ['name', 'email', 'message'];

    protected static function boot()
    {
        parent::boot();
        static::updating(function ($contact) {
            if ($contact->status == 1) {
                Mail::send(
                    'email',
                    array(
                        'name' => $contact->name,
                        'email' => $contact->email,
                        'bodyMessage' => $contact->message
                    ),
                    function ($message) use ($contact) {
                        $message->from($contact->email);
                        $message->to('email_address@email.com', 'Name')->subject('Your request status updated');
                    }
                );
            }
        });
    }
}

Voyager just makes your work easy. It creates the model and the controller for you in laravel. You will find the controller and the models in the same place as a custom model or controller created using artisan. I hope it helps.

If you have more questions, please be specific about the issue you are facing.

Cheers

You could very simply hook into the lifecycle of the Coupon model with an event listener:

https://laravel.com/docs/5.6/eloquent#events

First tell the model to fire the CouponCreated event when a new model is created

use App\Events\CouponCreated;

class Coupon extends Model
{
    /**
     * The event map for the model.
     *
     * @var array
     */
    protected $dispatchesEvents = [
        'created' => CouponCreated::class
    ];
}

That event will be passed the Coupon model which you can use to send the email.

If you want more specific help you'd need to post code. I am sure you could also just do this in the create method of the CouponController if you hunt that down.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM