首页 文章

Laravel 5.3通知发送错误

提问于
浏览
0

我试图在我的Laravel 5.3应用程序中使用Notifications . 我的第一个是在新注册后立即尝试发送欢迎通知 . 阅读文档并遵循"What's new in Laravel 5.3"教程后,我在保存记录后收到 "Call to undefined method Illuminate\Notifications\Notification::send()" 错误 .

以下信息是我尝试过的最新内容,但我尝试的一切都失败了 . 当我将通知放在create方法下面时,我得到一个保存,但没有发送通知 . 我把它放在前面只是为了看看会发生什么,因此错误 .

这是欢迎:

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

RegisterController:

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Notifications\Notification;
use App\Notifications\WelcomeToMyApp;

class RegisterController extends Controller
{


    use RegistersUsers;


    protected $redirectTo = '/home';


    public function __construct()
    {
        $this->middleware('guest');
    }


    protected function validator(array $data)
    {
        return Validator::make($data, [
            'familyname' => 'required|max:255|unique:users',
            'email' => 'required|email|max:255|unique:users',
            'phone' => 'required|unique:users',
            'password' => 'required|min:6|confirmed',
        ]);
    }


    protected function create(array $data)
    {
        Notification::send($data, new WelcomeToDStrokeTennis($data));

        return User::create([
            'familyname' => $data['familyname'],
            'email' => $data['email'],
            'phone' => $data['phone'],
            'password' => bcrypt($data['password']),
        ]);

    }
}

更新:似乎我没有得到User实例 . 我认为这是因为类型数组 . 我试图将新用户数据收集到$ user变量中,但它现在抛出错误:'对数组调用成员函数'notify()' . 所以我想我仍然没有得到正确的类型 .

protected function create(array $data)
{
    $user = collect(User::create([
        'familyname' => $data['familyname'],
        'email' => $data['email'],
        'phone' => $data['phone'],
        'password' => bcrypt($data['password']),
    ]))->all();

     $user->notify(new WelcomeToMyApp($user));

     return $user;

}

更新:我仍在尝试查找用户的实例 . 我的最新尝试:

protected function create(array $data)
{
        User::create([
        'familyname' => $data['familyname'],
        'email' => $data['email'],
        'phone' => $data['phone'],
        'password' => bcrypt($data['password']),
    ]);
    $user = User::orderBy('created_at', 'desc')->first();
    $user->notify(new WelcomeToMyApp($user));
    return $user;


}

我收到错误:未定义的属性:App \ Notifications \ WelcomeToMyApp :: $ id .

更新......快乐的假期!

我在做dd($ users)时显示以下数据 . 我在通知中添加了$ data参数 . 我收到错误:

RegisterController.php第66行中的FatalThrowableError:类型错误:传递给App \ Http \ Controllers \ Auth \ RegisterController :: create()的参数2必须是App \ User的实例,没有给出,在/ home / ubuntu / workspace /中调用第33行上的vendor / laravel / framework / src / Illuminate / Foundation / Auth / RegistersUsers.php

dd($user)

protected function create(array $data, User $user)
{
        User::create([
        'familyname' => $data['familyname'],
        'email' => $data['email'],
        'phone' => $data['phone'],
        'password' => bcrypt($data['password']),
    ]);
    $user = User::orderBy('created_at', 'desc')->first(); 
    //dd($user);
    $user->notify(new WelcomeToMyApp($data));
    return $user;

}

class WelcomeToDStrokeTennis extends Notification

{使用Queueable;

protected $user;
protected $data;

/**
 * Create a new notification instance.
 *
 * @return void
 */
public function __construct(array $data, User $user)
{
    $this->data = $data;
    $this->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)
                ->subject('MyApp Welcomes You.')
                ->greeting('You are now a registered user.')
                ->line('If you have not done so, please login and enter your player profile information')
                ->action('Login To D`Stroke Tennis', 'http://dstroketennis.com')
                ->line('Please call, text, or email if you have any problems or questions.');
}

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

}

解:

protected function create(array $data)
{
     $user = User::create([
        'familyname' => $data['familyname'],
        'email' => $data['email'],
        'phone' => $data['phone'],
        'password' => bcrypt($data['password']),
    ]);

    $user->notify(new WelcomeToMyApp($user));
    return $user;        
}

必须从通知类中删除参数,但这是用于此目的的代码 .

2 回答

  • 1

    通知外观使用需要第一个参数作为可通知用户的集合,而不是提供请求数据,您应该在下面传递用户集合更改

    $user = User::create([
            'familyname' => $data['familyname'],
            'email' => $data['email'],
            'phone' => $data['phone'],
            'password' => bcrypt($data['password']),
        ]);
    
       $user->notify(new WelcomeToDStrokeTennis($data));
        return $user;
    
  • 0

    这可能是因为 Notification 是一个门面 . 尝试使用

    use Notification;
    

    代替,

    use Illuminate\Notifications\Notification;
    

相关问题