php - preg_split() 期望参数 2 是字符串,给定对象
问题描述
我在我的用户和学生雄辩上使用一对多的关系。我正在使用 Laravel 的通知向用户发送邮件。
我正在尝试发送邮件,内容是那天出生的人。关系工作得很好,我在学生表中有单独的 user_id 列,该用户被创建到该学生。并且在发送邮件时,它应该只发送由经过身份验证的用户创建的人。
但是当我登录时,出现错误:preg_split() 期望参数 2 是字符串,给定对象
那是我按日期过滤用户的代码
$user->students()->where('birth_date','=', Carbon::today())
我如何调用toMail方法
$user->notify(new BirthdayReminder())
(在 LoginController 中经过身份验证)
我的toMail方法
public function toMail($notifiable)
{
$user = User::findOrFail(auth()->user()->id);
return (new MailMessage)
->from('admin@site.com')
->line('Hello, '.User::first()->name.'!')
->line('Today is birthday of:')
->line($user->students()->where('birth_date','=', Carbon::today()));
}
这里有什么问题?
解决方案
问题(可能)在line($user->students()->where('birth_date','=', Carbon::today()))
这将尝试将结果与结果放在一起,$user->students()->where('birth_date','=', Carbon::today())
但此结果是学生的集合。
如果您想获取所有名称,请执行以下操作:
line($user->students()->where('birth_date','=', Carbon::today())->pluck('name')->implode(','));
这将返回所有学生姓名的逗号分隔字符串。
您可以以任何您想要的方式操纵这些结果,但关键是输入的最终结果line(...)
需要是一个字符串
推荐阅读
- c# - Expression.Call 嵌套函数
- c++ - 指向从多个基类继承的派生类的基指针
- swift - Swift - 视觉格式语言 - 如何制作多行和多列
- python - 无法获取解开地图上某些点的所有名称
- mongodb - 在 Spring Data 中消除编译类的歧义
- android - Android NDK 系统不匹配 - Android 和 Windows
- javascript - 如何重命名所有级别的 JSON 嵌套对象中的键?
- regex - 在 bash 中使用正则表达式的意外结果
- php - GTFS-RT txt 文件作为 JSON 数据
- r - 如何对 R 中的一维数据执行卡方的精确检验?