Bhavin Shah Bhavin Shah - 4 months ago 14
PHP Question

Getting Error as htmlentities() expects parameter 1 to be string, array given

$email_ids= DB::table('users')->pluck('email_id');
$name = DB::table('users')-> pluck('name');

foreach ($email_ids as $email_id ) {
Mail::send('mail', ['user' => $name ] , function ($message) use ($email_id) {
$message->from('abc@xyz.com', 'ABC');
$message->to($email_id)->subject("Welcome");
});
}


I want to send a mail to respective users with their names in email. I get
$email_ids
and
$name
in array. but when I run this code , I get error as


htmlentities() expects parameter 1 to be string, array given


When I replace
'user' => $name
with
'user' => $email_id
. I runs successfully.

When I send mail it should be like

Hello , $name(name of user)


Thank You

Answer

Your code should be:-

$users = DB::table('users')->get(); // get all users
foreach ($users as $user ) { 
   $name =  $user->name; // get user's name
   $email_id =  $user->email_id; // get user's email
   Mail::send('mail', ['user' => $name ] , function ($message) use ($email_id) {
        $message->from('abc@xyz.com', 'ABC');
        $message->to($email_id)->subject("Welcome");
   });    
}

Note:- You can get specific columns from DB table via below query,

$users = DB::table('users')->select('name', 'email_id')->get();
Comments