简体   繁体   中英

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

<?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

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

You can get the user through your $request object, or through the Auth::user() method.

Provide an email

You need to setup an email where the notification should be sent to!

Add in your User model something like this:

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

For more information, check out the corresponding doc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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