简体   繁体   English

Laravel通知发送

[英]Laravel Notification Send

i have make the notification class.when user log in the notification show Welcome.when we send the notification shoe error Call to a member function routeNotificationFor() on string SurveyNotification code here 我已经创建了通知类。当用户登录通知显示欢迎时。当我们发送通知鞋错误时,在此处对字符串 SurveyNotification代码调用成员函数routeNotificationFor()

<?php
namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use App\User;

class WelcomeToDStrokeTennis extends Notification
{
    use Queueable;

    protected $user;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct(User $user)
    {


    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->line('MyApp Welcomes You.')
            ->action('Login To MyApp',         'http://dstroketennis.com')
            ->line('Thank you for trusting MyApp!');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
        //
        ];
    }
}

My Log in controller where we call the notification send 我的登录控制器,我们称之为通知发送

 public function login(Request $request)
{
    $this->validateLogin($request);
    Notification::send($request->toArray(), new WelcomeToSurvey($request->toArray()));
    // If the class is using the ThrottlesLogins trait, we can automatically throttle
    // the login attempts for this application. We'll key this by the username and
    // the IP address of the client making these requests into this application.
    if ($this->hasTooManyLoginAttempts($request)) {
        $this->fireLockoutEvent($request);

        return $this->sendLockoutResponse($request);
    }

    if ($this->attemptLogin($request)) {
        return $this->sendLoginResponse($request);
    }

    // If the login attempt was unsuccessful we will increment the number of attempts
    // to login and redirect the user back to the login form. Of course, when this
    // user surpasses their maximum number of attempts they will get locked out.
    $this->incrementLoginAttempts($request);

    return $this->sendFailedLoginResponse($request);
}

Setup user 设置用户

First of all, you need to set up an user (or another Model that extends the Notification class, so the notification knows "where to go". You can do that through your constructor, or by simply using 首先,您需要设置一个用户(或另一个扩展Notification类的模型,以便通知知道“去哪里”。您可以通过构造函数或简单地使用

$user->notify(new WelcomeToSurvey($request->toArray()));

You can get the user through your $request object, or through the Auth::user() method. 您可以通过$request对象或Auth::user()方法获取Auth::user()

Provide an email 提供电邮

You need to setup an email where the notification should be sent to! 您需要设置一封将通知发送到的电子邮件!

Add in your User model something like this: 在您的User模型中添加以下内容:

public function routeNotificationForMail()
    {
        return $this->email_address; //You e-mail property here
    }

For more information, check out the corresponding doc 有关更多信息,请查看相应的文档

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

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