Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 5.5-发送电子邮件-Can';无法获取非对象的属性_Laravel - Fatal编程技术网

Laravel 5.5-发送电子邮件-Can';无法获取非对象的属性

Laravel 5.5-发送电子邮件-Can';无法获取非对象的属性,laravel,Laravel,我正试图从一个像这样的Laravel 5.5控制器发送电子邮件 $user = User::find(1)->toArray(); Mail::send('emails.invite', $user, function($message) use ($user) { $message->to($user->email); $message->from('me@example.com'); $message-&

我正试图从一个像这样的Laravel 5.5控制器发送电子邮件

    $user = User::find(1)->toArray();
    Mail::send('emails.invite', $user, function($message) use ($user) {
        $message->to($user->email);
        $message->from('me@example.com');
        $message->subject('Test Subject');
    });
此操作失败,错误为

"message": "Trying to get property of non-object",
如果我将数组回显到主题中,我可以看到我确实有正确的$user可用,但由于某些原因,当我尝试提取$user->email时,它不喜欢它


有人有什么想法吗?

您正在调用
->toArray()
User
对象,因此它不再是一个对象

$user = User::find(1); // Assuming you are already protecting yourself from potentially not finding a user
$viewData = $user->toArray(); // Or better still don't expose the underlying structure
Mail::send('emails.invite', $viewData, function($message) use ($user) {
    $message->to($user->email);
    $message->from('me@example.com');
    $message->subject('Test Subject');
});

您的
$user
数组的类型
而不是您预期的
对象
,因为在返回的模型上调用了
toArray()
。更改该行并删除所述的
toArray()
,使其保持不变,或者使该行:

$message->to($user->email);
看起来更像:

$message->to($user['email']);

使用正确的数组元素引用。

看起来应该可以工作,但给了我。。。类型错误:传递给Lightning\\Mail\\Mailer::send()的参数2必须是数组类型,给定对象,
Mail::send('emails.invite',$user->toArray()
数据应为数组。