You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I create a new model from user input formwith pictures upload. I want to sent email notification out with picture attachements using model's created event. However the getMedia return null in the mailable. Here are my codes.
Model creation:
public function store(StoreBdReport $request)
{
$data = $request->safe()->only(['plant_id', 'cost_center_id', 'bd_priority_id', 'bd_equipment_id', 'bd_type_id', 'reported_bd_description']);
$data = array_merge($data, ['created_by_id' => $request->user()->id, 'bd_status_id' => 1]);
$bdReport = new BdReport($data);
//Add pictures to model media
if ($pictures = $request->file('pictures')) {
foreach ($pictures as $picture) {
$bdReport->addMedia($picture)->toMediaCollection();
}
}
$bdReport->save();
//Retrieve query string stored in create method.
$qryStr = $request->session()->pull('qryStr', '');
return redirect()->route('bdr.bd_reports.index', $qryStr);
}
Attachments in the mailable: getMedia return nothing here.
public function attachments(): array
{
$mediaItems = $this->bdReport->getMedia();
$attachments = [];
foreach ($mediaItems as $mediaItem) {
$attachments[] = $mediaItem;
}
return $attachments;
}
I got a workaround by calling BdReportCreated::dispatch($bdReport); after save() statement and remove the created event listener from the model. It works but not ideal.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I create a new model from user input formwith pictures upload. I want to sent email notification out with picture attachements using model's created event. However the getMedia return null in the mailable. Here are my codes.
Model creation:
Attachments in the mailable: getMedia return nothing here.
I got a workaround by calling BdReportCreated::dispatch($bdReport); after save() statement and remove the created event listener from the model. It works but not ideal.
How can I make media avialable in the event?
Beta Was this translation helpful? Give feedback.
All reactions