简体   繁体   English

Laravel 5.2-将数据传递到电子邮件视图

[英]Laravel 5.2- Passing data to email view

I want to sent some user details through mail using laravel Mail. 我想使用laravel Mail通过邮件发送一些用户详细信息。 But while I click sent mail button, it's saying following error. 但是当我点击发送邮件按钮时,它说的是跟随错误。

ErrorException in f007458b7fd9880a4f1d9bd9b684d2f236a70b32.php line 8: Trying to get property of non-object (View: C:\\xampp\\htdocs\\laravel-roles\\resources\\views\\mail.blade.php) f007458b7fd9880a4f1d9bd9b684d2f236a70b32.php中的ErrorException第8行:尝试获取非对象的属性(查看:C:\\ xampp \\ htdocs \\ laravel-roles \\ resources \\ views \\ mail.blade.php)

Here is the controller: 这是控制器:

 public function sendEmail($id)
    {
        $user = User::findOrFail($id);

        Mail::send('mail', ['user' => $user], function ($m) use ($user) {
              $m->from('xxx@gmail.com', 'xxx');

            $m->to($user->email, $user->name)->subject('Thanks!');
        });
    }

here is the route: 这是路线:

Route::get('/sendMail/{id}',[
        'uses' =>'AppController@sendEmail',
        'as' =>'sendMail',
        'middleware'=>'roles',
        'roles'=>['Admin']
        ]);

Here is the mail.blade.php file: 这是mail.blade.php文件:

    <html>
    <head></head>
    <body>
        Dear,
            Thanks for registering for the course.
            Login Details:
            @foreach($user as $row)
            UserName: {{$row->email}}
            Password: {{$row->phone}}
            @endforeach
    </body>
</html>

Here is the mail sender button in view page: 这是视图页面中的邮件发件人按钮:

@foreach($user as $row)
<a href="{{route('sendMail',$row->id)}}" class="btn btn-warning">Complete</a> 
  @endforeach

The route you are trying to access does not exists. 您尝试访问的路由不存在。 It most likely has nothing to do with your mail function. 它很可能与您的邮件功能无关。

Please check routes file or post it here and let us know which route you are accessing. 请检查路线文件或在此处发布,并告诉我们您正在访问的路线。

In your controller, you used: 在您的控制器中,您使用了:

$user = User::findOrFail($id);

Which should return a single user object! 哪个应该返回一个用户对象!

That means in your view, you can't use foreach for that user. 这意味着在您的视图中,您不能为该用户使用foreach。 To access that user data, you can just write 要访问该用户数据,您只需编写即可

Login Details:
UserName: {{$user->email}}
Password: {{$user->phone}}

That should work for you. 这对你有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM