我正在尝试通过laravel中的通知传递关系对象。我有一些实践,可以有很多医生,而那些医生只能属于一个实践。它们通过Doctors表上的Foreign_id字段作为practice_id
连接。当某人创建医生时,他们通常会同时创建诊所。但是,他们也有可能通过选择表格选择现有的做法。
有人创建新做法时,我可以将$doctor
和$practice
完美地传递给通知。但是,当他们选择现有做法时,我似乎无法称呼现有的$practice
通过。这是我的代码:
NewDoctor通知
return (new MailMessage)
->subject('MedReps Doctor Registration')
->line('Hello,')
->line('A new doctor has been registered:')
->line(' ')
->line($this->practice->name)
->line($this->practice->address)
->line(
$this->practice->city
. ', '
. $this->practice->state
. ' '
. $this->practice->zip
)
->line('Phone: ' . $this->practice->phone)
->line('Fax: ' . $this->practice->fax)
->line('Contact: ' . $this->practice->contact)
->line('Email: ' . $this->practice->email)
->line(' ')
->line(
$this->doctor->first_name
. ' '
. $this->doctor->last_name
. ', '
. $this->doctor->type
)
->line('NPI: ' . $this->doctor->npi)
->line('License: ' . $this->doctor->license)
->line($dea_line);
DoctorController
// If the practice already exists
if ($request->create_doctor_practice_type == 'existing') {
$doctor = new Doctor();
$doctor->practice_id = $request->practice;
... data ...
$doctor->save();
// Get Practice information for notification
$practice = Practice::select('id')
->where('id', $request->practice)
->first();
// Send notification
Notification::route('mail', 'jeremy@medrepsrx.com')
->notify(new NewDoctor($doctor, $practice));
// Create a new practice
} else {
$practice = new Practice();
... data ...
$practice->save();
$doctor = new Doctor();
... data ...
$doctor->save();
// Send notification
Notification::route('mail', 'jeremy@medrepsrx.com')
->notify(new NewDoctor($doctor, $practice));
}
答案 0 :(得分:1)
更改
$practice = Practice::select('id') ->where('id', $request->practice) ->first();
收件人
$practice = Practice::find($request->practice);
创建新练习时,会将整个练习对象发送到通知中,但是当该对象已经存在时,您只是在发送ID。