如何解决对数组成员函数notify()的调用?(laravel 5.3)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41625733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:06:39  来源:igfitidea点击:

How to solve Call to a member function notify() on array? (laravel 5.3)

phplaravelsendmaillaravel-5.3

提问by moses toh

My listener is like this :

我的听众是这样的:

<?php

namespace App\Listeners;

use App\Events\CheckoutOrderEvent;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Mail;

class CheckoutOrderListener
{
    public function __construct()
    {
        //
    }
    public function handle(CheckoutOrderEvent $event)
    {
        // dd($event);
        $event->data->notify(New \App\Notifications\CheckoutOrder($event->data));
    }
}

If I run dd($event), the result is like this :

如果我运行 dd($event),结果是这样的:

enter image description here

在此处输入图片说明

When executed, there exist error : Call to a member function notify() on array

执行时,存在错误:调用数组上的成员函数notify()

How to solve it?

如何解决?

回答by Alexey Mezenin

You need to use notify()on a model with Illuminate\Notifications\Notifiabletrait, but definitely not on an array.

您需要notify()在具有Illuminate\Notifications\Notifiable特征的模型上使用,但绝对不能在数组上使用。

For example, you can get an instance of a Userfirst:

例如,您可以获得 a Userfirst的实例:

$user = User::where('email', $event->data['email'])->first();

And then use notify():

然后使用notify()

$user->notify(....)

回答by Vipertecpro

Alright, I was trying to send email to end-user of the application for activating their account (Provided in registration form ) then i got this Error because i was passing "ID" to notify(), actually it should pass whole details of users. enter image description here

好的,我试图向应用程序的最终用户发送电子邮件以激活他们的帐户(在注册表中提供)然后我收到此错误,因为我将“ID”传递给 notify(),实际上它应该传递用户的全部详细信息. 在此处输入图片说明

Solved By changing in Http/Controller/Admin/Auth/RegistrationController.php

通过在 Http/Controller/Admin/Auth/RegistrationController.php 中更改解决

enter image description here

在此处输入图片说明

And that's my Notification Class: app/Notification/UserActivate.php Look at my

这就是我的通知类:app/Notification/UserActivate.php 看看我的

__construct() and toMail($notifiable) methods

__construct() and toMail($notifiable) methods

enter image description here

在此处输入图片说明

I hope my mistake helped you. :)

我希望我的错误对你有帮助。:)