php 如何发送消息电子邮件?

3zwtqj6y  于 2023-01-16  发布在  PHP
关注(0)|答案(1)|浏览(122)

我想发邮件给非常用户,这是管理员。但这个代码显示我错误Call to undefined method Illuminate\Database\Eloquent\Builder::all()。所以我怎么能发送每一个管理员的电子邮件消息?

public function index()
{
    $user = User::where('role', 'Admin')->all();

    $project = [
        'greeting' => 'Hi '.$user->login.',',
        'body'       => 'This is the project assigned to you.',
        'thanks'     => 'Thank you this is from codeanddeploy.com',
        'id'         => 1
    ];

    $user->notify(new EmailNotification($project));
    return view('customers', [
        'customers' => Customer::all()
    ]);
}
0yg35tkg

0yg35tkg1#

试试这个:

public function index()
{
    // notify a single user
    $user = User::where('role', 'Admin')->first();

    $project = [
        'greeting' => 'Hi '.$user->login.',',
        'body'       => 'This is the project assigned to you.',
        'thanks'     => 'Thank you this is from codeanddeploy.com',
        'id'         => 1
    ];

    $user->notify(new EmailNotification($project));
    return view('customers', [
        'customers' => Customer::all()
    ]);
}

要通知多个用户,请尝试

$users = User::where('role', 'Admin')->get();

foreach($users as $user) {
   $user->notify(new EmailNotification($project));
}

相关问题