将数据保存到数据库后发送电子邮件

时间:2019-06-03 08:25:05

标签: php laravel

在将数据从控制器保存到数据库后,我试图从laravel应用程序发送电子邮件。我该怎么做?

这是我保存数据的代码。我想先发送电子邮件,然后再重定向到/vehicle路由

if($request->input('type_of_insurance') == 'third_party_insurance') {
    $motorVehicle = new MotorVehicle();
    $motorVehicle->Make = $request->input('car_make');
    $motorVehicle->Model = $request->input('car_model');
    $motorVehicle->Car_Value = $request->input('car_value');
    $motorVehicle->Year_Of_Manufacture = $request->input('car_year');
    $motorVehicle->Engine_Number = $request->input('engine_number');
    $motorVehicle->Chassis_Number = $request->input('chassis_number');
    $motorVehicle->Cubic_Capacity = $request->input('cubic_capacity');
    $motorVehicle->Type_Of_Insurance = $request->input('type_of_insurance');
    $motorVehicle->Stamp_Duty = $stampDuty;
    $motorVehicle->Standard_Fee = 50;
    $motorVehicle->Premium = $premium;
    $motorVehicle->save();

    return redirect('/vehicle')->with('message','Policy Approved');

1 个答案:

答案 0 :(得分:2)

在这种情况下,您可以在模型上使用观察者或创建观察者。


class MotorVehicleObserver
{
    public function created(Content $content){
        //this function will be called every time you insert new data on your database
        //your codes about sending the email will come here
    }
}

并将此观察者添加到您的模型中:


    protected static function boot()
    {
        parent::boot();
        self::observe(new MotorVehicleObserver);
    }

或者您可以将观察者直接添加到模型中,如下所示:


   protected static function boot()
    {
        parent::boot();

        static::created(function (self $content){
            //this function will called every time you insert a new data on your database
            //your codes about sending email will come here
        });
    }

有关更多信息,请访问:Laravel Events