You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel按需通知传递邮箱数组报错:无法读取数组的email属性

问题

我需要把User.php模型中筛选后的邮箱和姓名数组传给Laravel按需通知,但User模型里设置了protected $appends = ['profile_photo_url'],导致Arr::except()、Arr::only()这类助手函数没法移除这个字段,只能暂时注释掉。

之后我用下面的代码获取用户邮箱和姓名:

$users_ec_ = User::where('area_id',$area_value_especial->id)->get(['email','name'])->toArray();

用dd($users_ec_)得到的结果是:

[
  0 => array:1 [
      "email@mail.mx" => "JUán perez"
  ]
]

调用通知时执行:

Notification::route('mail',$users_ec_)
       ->notify(new Signed($eventos_ec));

Signed.php通知类代码如下:

<?php

namespace App\Notifications;

use App\Models\Meeting;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Collection;

class Firmadas extends Notification implements ShouldQueue
{
    use Queueable;

    private $meetings;
 
    public function __construct(Collection $meetings)
    {
        $this->meetings = $meetings;
    }


    public function via($notifiable)
    {
        return ['mail'];
    }


    public function toMail($notifiable)
    {
        $meetings = $this->meetings;
        return (new MailMessage)
            ->subject('New signatures...')
            ->markdown('emails.signed',compact('meetings'))
            ;
    }
}

这时出现错误:

[2022-09-28 09:23:44] local.ERROR: Attempt to read property "email" on array {"exception":"[object] (ErrorException(code: 0): Attempt to read property \"email\" on array at /home/vagrant/myProject/vendor/laravel/framework/src/Illuminate/Notifications/Channels/MailChannel.php:228)
[stacktrace]

解决方案

Laravel按需通知要求传入的数组格式为:以邮箱地址为键,姓名为值的数组。

修改代码如下,手动处理数组格式:

$users_ec_ = User::where('area_id',$area_e_c->id)->get(['email','name'])->toArray();
// 由于User模型的protected $appends包含profile_photo_url,数组助手函数失效,手动循环处理格式
$users_ec = [];
foreach ($users_ec_ as $user) {
    $users_ec[$user['email']] = $user['name']; // 符合按需通知要求的格式
}
// 传递格式化后的数组到通知
Notification::route('mail',$users_ec)
   ->notify(new Signed($meetings_ec));

问题即可解决。

内容的提问来源于stack exchange,提问作者Pathros

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 07:50:29