简体   繁体   English

Laravel/Voyager:使用 BREAD 创建新数据时发送电子邮件

[英]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.我有一个带有 Voyager 的优惠券表 BREAD,其中每个优惠券都有一个电子邮件地址。 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.例如从上图中,我创建了名为 haha​​123 的优惠券后,我想发送一封电子邮件到 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.由于我没有制作用于生成新优惠券的自定义控制器,并且一直只使用 Voyager 的默认 BREAD 功能,因此我不确定应该在哪里以及如何操作。

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.因此,我只是通过添加我自己的自定义 voyager CouponsController 来覆盖默认控制器并在自定义 CouponsController 内的 store 方法中添加 Laravel 邮件功能来实现这一点。

Maybe helpful, Send email when model status updated (Laravel Voyager Admin)也许有用,当模型状态更新时发送电子邮件(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. Voyager 只是让您的工作变得轻松。 It creates the model and the controller for you in laravel.它在 Laravel 中为您创建模型和控制器。 You will find the controller and the models in the same place as a custom model or controller created using artisan.您会在与使用 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:您可以非常简单地使用事件侦听器连接到 Coupon 模型的生命周期:

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

First tell the model to fire the CouponCreated event when a new model is created首先告诉模型在创建新模型时触发CouponCreated事件

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.该事件将传递Coupon模型,您可以使用它来发送电子邮件。

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.我相信如果你找到它,你也可以在 CouponController 的create方法中做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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