无法捕获所有参数

时间:2017-01-02 05:20:39

标签: arrays laravel-5.3 laravel-eloquent

好的,我正在尝试创建记录并发送电子邮件通知/确认记录已被记录。我在玩家模型和Lessonhours模型之间有很多关系。 User模型具有一对多的Players模型。 我经营过我的商店'方法有几个不同的修改,我终于收到我的电子邮件发送。问题是我无法获得存在的玩家阵列并向每个玩家发送电子邮件。我的表单中的多个选项正在各自的表中正确插入。在收集电子邮件数据时,我最接近以下代码。这个问题是我只有一个玩家而不是两个或更多玩家。我希望这是有道理的。屏幕截图如下。

$ request数组的代码和截图: Code and dd() for $request object.

The dd($request->players)

After query, not capturing the array of players.

Not capturing array of players dd($players)

Code to add an index to the $request object

Error message after trying to add index.

玩家型号: Players Model

我对此并不十分熟悉,并且我发现很难确定使用哪个文档示例。如何在插入记录后获取发送的所有电子邮件地址?我感谢所有提供的帮助。

Lessonhours模型: Lessonhours Model

存储Lessonhours表单 Lessonhours form

用户模型 enter image description here

1 个答案:

答案 0 :(得分:0)

我不确定你为什么要在数组中为whereIn子句放置一个数组,但如果请求播放器已经作为一个数组出现,那么你应该能够做到:< / p>

whereIn('id', $request->players)

接下来,您只会发送一封电子邮件,因为您已从循环内部返回,因此它会向第一位玩家发送一封电子邮件,然后返回回复。要保存有一个您不再使用的临时变量,您可以执行以下操作:

Players::with('users')->whereIn('id', $request->players)->get()
    ->each(function ($player) use ($lesshours) {
        Mail::to($player->users)->send(new ThankYouForLessonPackagePurchase($lesshours));
    });

return back()->with(['success' => 'Player is enrolled in new Lesson Package.']);

你不必做上述事情,这只是另一种写作方式。

最后,如果您总是要求发送播放器ID,那么您也可以将其添加到验证阵列中:

'players    => 'required',
'players.*' => 'exists:players,id',

希望这有帮助!