Отправка электронной почты после сохранения данных в базе данных - PullRequest
0 голосов
/ 03 июня 2019

Я пытаюсь отправить электронное письмо из моего приложения 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 Ответ

2 голосов
/ 03 июня 2019

в этом случае вы можете использовать наблюдателя в вашей модели или создать его.


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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...