0

I'm trying to send an email to multiple emails, so I did a query to my users tables but the result is an array with keys that don't work mail->to().

I need an array like this: $owners = ['[email protected]', '[email protected]','[email protected]']; from database.

My query:

$owners = DB::table('users')->select('email')->where('active', 1)->where('userType', 1)->get();

I also try use ->toArray() but comes with keys.

Email:

Mail::send('email-view', $data, function($message) use ($data, $owners)
{
  $message->from('[email protected]' , 'FROM');
  $message->to($owners)->subject('subject');

});
2
  • please check my answer. it might be resolve your problem. Thanks. Commented Mar 22, 2018 at 11:47
  • $query->where([ ['column_1', '=', 'value_1'], ['column_2', '<>', 'value_2'], [COLUMN, OPERATOR, VALUE], ... ]) Commented Mar 22, 2018 at 11:55

4 Answers 4

1
$owners = DB::table('users')->where('active', 1)->where('userType', 1)->pluck('email')->toArray(); 
Sign up to request clarification or add additional context in comments.

7 Comments

lists is now pluck.
Oh! yes Modified it...@u_mulder. Thanks for your suggestion.
returns me Illegal offset type @DarshanJain
which laravel version you are using @user3242861 ?
5.4 @DarshanJain
|
1

You can use ->pluck(), like this:

$owners = DB::table('users')->select('email')->where('active', 1)->where('userType', 1)->get();
$emailList = $owners->pluck('email');

1 Comment

Returns me Illegal offset type @Phiter
0
$owners = DB::table('users')->where('active', 1)->where('userType', 1)->pluck('email');

or

    $owners = User::where('active', 1)->where('userType', 1)->pluck('email');

here User is your model name

Comments

0

First of all, the where condition is not proper in your query builder code. It has to be like:

$query->where([
    ['column_1', '=', 'value_1'],
    ['column_2', '<>', 'value_2'],
    [COLUMN, OPERATOR, VALUE],
    ...
])

You can use pluck() to get the list in Laravel 5.

All collections also serve as iterators, allowing you to loop over them as if they were simple PHP arrays:

    foreach ($owners as $owner) {
        echo $owner->filed_name;
        echo $owner->filed_name;
        ...
    }

There is no restriction for using the core PHP code while using a PHP framework. You may refer the official PHP Arrays Manual page to work with the language construct.

If you need to convert JSON to array, use:

json_decode($owners);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.