【问题标题】:Laravel - Multiple Arrays for Multiple Recipients (Mailable/Notifications)Laravel - 多个收件人的多个数组(可邮寄/通知)
【发布时间】:2018-05-15 20:19:40
【问题描述】:

我正在为一个项目和一个有趣的困境寻求建议。

我的货运模型中有多个字段,它们是:

  • shipto_id
  • shipfrom_id
  • billto_id

它们都(通过不同的关系)链接到我的客户模型:

public function billtoAccount()
{
    return $this->belongsTo('App\Customer', 'bill_to');
}

public function shiptoAccount()
{
    return $this->belongsTo('App\Customer', 'ship_to');
}

public function shipfromAccount()
{
    return $this->belongsTo('App\Customer', 'ship_from');
}

而这些客户(实际上可能更适合描述为客户帐户(这些不是用户帐户,它们更像是与开展业务的每家公司的个人资料))可以拥有大量与之相关联的用户他们。

 public function users()
{
    return $this->belongsToMany(User::class);
}

现在,虽然我知道如何发送邮件和通知,但我很想知道如何将这些发送到多个用户的电子邮件。所以让我来描述以下内容:创建了一些东西,并引用了以下客户(反过来,他们的用户)。

  • (billto_id) - 客户帐户 1 - 用户 1 (email1@example.com)
  • (shipto_id) - 客户帐户 2 - 用户 2 (email2@example.com) 和用户 3 (email3@example.com)
  • (shipfrom_id) - 客户帐户 37 - 用户 6 (email4@example.com)

现在,我将如何将用户的电子邮件转移到一组电子邮件中以向他们发送通知或邮件?

所以它应该会弹出:email1@example.com、email2@example.com、email3@example.com、email4@examples.com

【问题讨论】:

  • 你不能循环遍历这三个关系并从相关模型中获取电子邮件吗?可能想要摆脱这样的心态,即这应该是一种雄辩的关系,而不仅仅是一种执行这种逻辑的方法。

标签: laravel email laravel-5 notifications


【解决方案1】:

更好的选择:

Notification::send($shipment->getNotifiables(), new ShipmentWasCreated());

【讨论】:

    【解决方案2】:

    详细说明@Devon 的评论: 这是业务逻辑。您可以在 Shipment 模型上有一个方法,该方法返回要作为数组通知的客户实例,例如getNotifiables() : array

    然后在您的客户模型中,您可以使用 Notifiable trait

    use Illuminate\Notifications\Notifiable;

    并循环遍历您的通知,即客户

    $notification = new ShipmentWasCreated()
    foreach ($shipment->getNotifiables() as $notifiable) {
        $notifiable->notify($notification);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-02
      • 2011-11-15
      • 2011-03-18
      • 2017-04-12
      • 1970-01-01
      • 2019-02-24
      • 2021-06-27
      • 2019-04-22
      相关资源
      最近更新 更多