我想使用laravel通知将多个附件发送给用户,我现在可以发送一个文件,但我的方式不是动态方式,我试图使用foreach发送文件,但它只发送了第一个文件。
===========================
blade.php
<div class="col-sm-12 col-md-12">
<input type="file" name="files[]" class="dropify" accept=".pdf,.jpg, .png, image/jpeg, image/png"
/>
</div><br>
<div class="col-sm-12 col-md-12">
<input type="file" name="files[]" class="dropify" accept=".pdf,.jpg, .png, image/jpeg, image/png"
/>
</div>
<div class="col-sm-12 col-md-12">
<input type="file" name="files[]" class="dropify" accept=".pdf,.jpg, .png, image/jpeg, image/png"
/>
</div>控制器
if ($request->hasfile('files')) {
foreach ($request->file('files') as $file) {
$name = $file->getClientOriginalName();
$file->move(public_path('Attachments/' .), $name);
$data[] = $name;
$attachments = new TaskAttachment();
$attachments->file_name = $name;
$attachments->save();
}
//to send email
Notification::route('mail', $engineer_email)
->notify(new AddTaskWithAttachments($data));
}AddTaskWithAttachments.php静态方法
public function toMail($notifiable)
{
return (new MailMessage)
->subject(" new task")
->action('link', $url)
->attach(public_path('Attachments/'.$this->files[0]));
}带着
foreach($this->files as $file){
return (new MailMessage)
->subject(" new task")
->action('link', $url)
->attach(public_path('Attachments/'.$file));发布于 2022-04-20 11:59:27
foreach中的返回可以防止多次执行。你可以试一试,但我不确定它是否有效,也找不到文档:
$mailmessage = (new MailMessage)
->subject(" new task")
->action('link', $url);
foreach($this->files as $file){
$mailmessage->attach(public_path('Attachments/'.$file));
}
return $mailmessage;https://stackoverflow.com/questions/71938901
复制相似问题