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

Laravel中如何从jobs负载命令获取特定的tender_id数据?

获取Laravel通知中受保护属性tender_id的方法

方法一:添加公共访问器(推荐)

在App\Notifications\TenderInProgress类中新增一个公共方法来返回受保护的tender_id,这是符合面向对象封装规范的做法:

// App\Notifications\TenderInProgress.php
class TenderInProgress extends Notification
{
    // 原有代码...

    public function getTenderId()
    {
        return $this->tender_id;
    }
}

之后就可以直接调用该方法获取值:

dd($command->notification->getTenderId());

方法二:使用反射机制(无需修改原类)

如果无法修改TenderInProgress类的代码,可以通过PHP反射机制强制访问受保护属性:

$notification = $command->notification;

// 初始化反射类
$reflection = new ReflectionClass($notification);
// 获取目标属性
$property = $reflection->getProperty('tender_id');
// 设置属性为可访问
$property->setAccessible(true);
// 获取属性值
$tenderId = $property->getValue($notification);

dd($tenderId);

方法三:利用通知类的序列化方法

Laravel通知类通常会实现toArray或toDatabase方法来转换为数组格式,若你的TenderInProgress类中有相关实现,可以直接通过这些方法获取属性:

// 假设toArray方法中包含tender_id字段
$notificationData = $command->notification->toArray();
dd($notificationData['tender_id']);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 07:05:04