首页 > 解决方案 > 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()));
    }

这里有什么问题?

标签: phpsqllaravel

解决方案


问题(可能)在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(...)需要是一个字符串


推荐阅读