I want to send multiple attachments to a user by using laravel notifications, I can send now one file but my way is not a dynamic way , I tried to send the files by using foreach but it sent only the first file
===========================
blade.php
<div >
<input type="file" name="files[]" accept=".pdf,.jpg, .png, image/jpeg, image/png"
/>
</div><br>
<div >
<input type="file" name="files[]" accept=".pdf,.jpg, .png, image/jpeg, image/png"
/>
</div>
<div >
<input type="file" name="files[]" accept=".pdf,.jpg, .png, image/jpeg, image/png"
/>
</div>
controller
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 [static way]
public function toMail($notifiable)
{
return (new MailMessage)
->subject(" new task")
->action('link', $url)
->attach(public_path('Attachments/'.$this->files[0]));
}
with foreach
foreach($this->files as $file){
return (new MailMessage)
->subject(" new task")
->action('link', $url)
->attach(public_path('Attachments/'.$file));
CodePudding user response:
The return in the foreach prevents multiple executions. You could try this, but I'm not sure it works, couldn't find it documented:
$mailmessage = (new MailMessage)
->subject(" new task")
->action('link', $url);
foreach($this->files as $file){
$mailmessage->attach(public_path('Attachments/'.$file));
}
return $mailmessage;