【问题标题】:How to Create Model with Notifiable Trait如何创建具有可通知特征的模型
【发布时间】:2019-10-27 17:41:21
【问题描述】:

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

首先,在我的控制器中:

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

 $u3 = new User3($collection);        

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

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

我的 User3 班级是:

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

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

【问题讨论】:

  • 很难在没有错误消息的情况下提供帮助。
  • 对不起,我将一个集合传递给 User3 并返回相同的集合,并将作为集合的输出传递给我的通知参数,对吗?
  • 如果您希望在多个模型上完成我们可以解决的问题,请仅传递模型

标签: laravel laravel-notification


【解决方案1】:

Notification send 需要一个 Notifiable 对象,而不是电子邮件列表本身,如果您将其更改为此,您应该会更进一步。

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

【讨论】:

    猜你喜欢
    • 2022-06-14
    • 2013-11-22
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    • 1970-01-01
    • 2017-08-17
    • 2018-06-21
    • 2023-02-23
    相关资源
    最近更新 更多