如何解决array_merge():预期参数 1 为数组,使用集合作为事件参数时给出的对象
现在我使用的是 Laravel 8.26 和 Pusher 4.1。
这是我的活动:
class NotifSeller implements ShouldBroadcast
{
use Dispatchable,InteractsWithSockets,SerializesModels;
public $fields;
public function __construct($fields)
{
$this->fields = $fields->toArray();
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
// return new PrivateChannel('notif-seller.'.$this->fields->seller_id);
return new PrivateChannel('notif-seller.'.$this->fields->seller_id);
}
public function broadcastWith()
{
$message = $this->fields;
return $message;
}
}
这是我的控制器:
$t = new Transaksi();
$t->item_id = $request->item_id;
$t->seller_id = $request->seller_id;
$t->buyer_id = $request->buyer_id;
$t->category = 'merchandise';
$t->amount = $request->amount;
$t->save();
event(new NotifSeller($t));
return redirect()->back()->with('status','Success');
它会显示错误信息
array_merge(): Expected parameter 1 to be an array,object given
我这里的代码有误吗?我查了很多教程,在他们的教程中他们可以使用集合作为事件参数,但是当我尝试时,它变成了这样。
对不起,如果我的英语不好,我不是英语母语,这是我第一次在 Stack Overflow 上提问,所以我希望你能理解这一点。提前致谢。
解决方法
很可能是因为您正在使用 SerializesModels
Laravel 尝试序列化您的 public $fields;
道具,因为它包含一个模型。
不同的潜在解决方案:
- 尝试输入道具
public array $fields;
- 在那里保存整个模型
public Transaksi $fields;
- 删除
SerializesModels
特性,技术上不需要。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。