如何创建具有可通知特征的模型

How to Create Model with Notifiable Trait

我想创建一个具有可通知功能的模型,

首先,在我的控制器中:

$collection = collect([
    [
    'name' => 'user1',
    'email' => 'user1@gmail.com',
    ],
    [
    'name' => 'user2',
    'email' => 'user2@gmail.com',
    ],
    [
    'name' => 'user1000',
    'email' => 'user1000@gmail.com',
   ],
 ]);

 $u3 = new User3($collection);        

当我 return $u3->getEmailList(); ,输出为:

[{"name":"user1","email":"user1@gmail.com"},{"name":"user2","email":"user2@gmail.com"},{"name":"user1000","email":"user1000@gmail.com"}]   

我的 User3 的 class 是:

namespace App;
use App\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Notification;
use Illuminate\Notifications\RoutesNotifications;
use Notifications\EmailClientOfAccount;

class User3 extends User
{
   use Notifiable;
   public $emailList;

  public function __construct($emails)
  {
        $this->emailList = $emails;

  }

  public  function getEmailList()
    {
     return $this->emailList;
    }
   public function routeNotificationForMail($notification)
   {
    return $this->emailList['email'];
   }
 }

然后,我将 $u3 传递给 Notification 为:

Notification::send($u3->getEmailList(), new 

SendMailNotification($template,$subject,$request->input('mailFromTitle'),$attachments));

显示以下错误:

Symfony\Component\Debug\Exception\FatalThrowableError: Call to a member function routeNotificationFor() on array

你能帮我解决这个问题吗?

提前致谢,

//-------------------- 我更正为:

Notification::send($u3, new SendMailNotification($template,$subject,$request->input('mailFromTitle'),$attachments));

在我的通知中:

public function toMail($notifiable)
{
return  new EmailTo($notifiable,$this->view,$this->topic,$this- 
      >mailFrom,$this->attaches);
}

在 Build() 中:

public function build()
    {

        $email= $this->view($this->view);
        return $email;
    }

但是不行,不知道哪里出错了?

Notification send需要一个Notifiable对象,而不是邮件列表本身,如果你改成这个,你应该更进一步。

Notification::send($u3, new SendMailNotification($template,$subject,$request->input('mailFromTitle'),$attachments));