You will have to set that up yourself. The created event gets called every time a model has been created.
But you can just have the same logic after your foreach() in your controller and then send the mail there.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi everyone, I wanted to make a observer that handles my events. Everything ok but there's a little problem that confused me. My problem is 'created' event. I'm storing lots of 'Date' model in one time on my controller and my idea is the when all Dates saved correctly, then send an email to X person releated with 'Date'.
here is my code; // On creating; change date format
public function creating(Date $date)
{
/*$dates = [];
foreach (request()->tarihler as $k => $tarih)
$dates[] = Carbon::createFromFormat('d/m/Y', $tarih)->format('Y-m-d');
request()->tarihler = [];
request()->merge(['tarihler' => $dates]);*/
}
// Send email
public function created(Date $date)
{
//dd(request()->all());
$user = $date->user()->first();
$student = $date->student()->first();
$teacher = $date->teacher()->first();
// currentPerm. returning 'normal' it's working good
if (CustomFunctions::instance()->currentPermission() != "normal")
{
CustomFunctions::mailer('teacher.mail.context', $teacher->email, 'ILKSMS', [
"params" => [
"name" => $teacher->name,
"email" => $teacher->email,
"phone" => $teacher->phone_number,
"mesaj" => "blabla."
]
]);
}
}
When I'm creating hundreds of Date in same time;
public fun. store(...)
foreach ($request->tarihler as $tarih){
$yeniTarih = Carbon::createFromFormat('d/m/Y', $tarih)->format('Y-m-d');
foreach ($request->randevular as $randevuSaati){
$date = Date::where('teacher_id',$request->teacher_id)
->where('date',$yeniTarih)
->where("time",$randevuSaati.":00")
->count();
if($date < 1){
Date::create([
'teacher_id' => $request['teacher_id'],
'school_detail_id' => $this->funcs->getSchoolDetailId(),
'date'=>$yeniTarih,
'time'=>$randevuSaati.":00",
]);
}
}
}
}
İt's sending hundres of mail because it's triggering Model has been saved and it has been saved hundreds time. Just send one time e-mail when it's completed insert all data
Please or to participate in this conversation.