how to collect only emails from related table

时间:2018-07-25 05:27:19

标签: laravel laravel-5 laravel-5.6

i want to send emails to client multiple email id i am collective email data like this

$emails = EmailClient::whereHas('client', function ($query) use($client){
                $query->where('id', $client->id);
            })->get();

Email Data

now i want to add collected email in array

$mail = Mail::to([$emails->email])->send(new SendInvoice($client));

Getting Error help me for sort out

1 个答案:

答案 0 :(得分:1)

对于单个电子邮件,您可以使用first方法

 $emails = EmailClient::whereHas('client', function ($query) use($client){
       $query->where('id', $client->id);
 })->select("email")->first();

如果您希望表格中的所有电子邮件都为数组,请使用pluck

$emails = EmailClient::whereHas('client', function ($query) use($client){
       $query->where('id', $client->id);
 })->pluck("email");

像这样通过

$mail = Mail::to($emails)->send(new SendInvoice($client));