Laravel通知发送

时间:2017-06-13 08:17:02

标签: laravel

我已经制作了通知类。当用户登录通知时显示Welcome.when我们发送通知鞋错误     在字符串上调用成员函数routeNotificationFor()     SurveyNotification代码在这里

<?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 [
        //
        ];
    }
}

我的登录控制器,我们称之为通知发送

 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);
}

1 个答案:

答案 0 :(得分:2)

设置用户

首先,您需要设置一个用户(或另一个扩展Notification类的模型,以便通知知道&#34;去哪里&#34;。 您可以通过构造函数或仅使用

来实现

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

您可以通过$request对象或Auth::user()方法让用户了解。

提供电子邮件

您需要设置发送通知的电子邮件地址!

User模型中添加以下内容:

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

有关详细信息,请查看corresponding doc