Laravel 多对多和一对多 Table 模型和控制器

Laravel Many to Many and One to Many Table Model and Controller

谁能帮我弄清楚这个 out.To 的开头是 不是 CRUD and relation between three tables/Models in Laravel 的副本

有 2 tables Medication_Patient 枢轴 Table 和 Med_Time

Medication_Patient 枢轴 Table

id       medication_id          patient_id
1         1 (MED X)             1 (Patient X)
2         2 (MED y)             1 (Patient X)
3         2 (MED y)             2 (Patient Y)

还有 MEdTime,它存储时间以及是否在何处给药

  id        med_patient_id(foreign key)    Day      time      given
   1              1                       Yesterday  0900        1
   2              1                       Today      0900        0
   3              1                       Today      2000        0
   4              2                       Today      0600        1 

关于我的模型

class Medication extends Model {
    protected $guarded = [];
    public function patient()
    {
        return $this->belongsToMany('App\Patient');
    } }


class Patient extends Model
{
public function medication()
    {
        return $this->belongsToMany('App\Medication');
    }
}

将药物分配给患者

 $assignedMeds = $patient->medication()->get();

但它没有给我 Pivot table 的 ID,我需要找到用药时间,所以我使用了 (如果有更好的方法,请告诉我为此)

//get the id from medication_patient pivot Table 



 $medPatient = DB::table('medication_patient')->select('id')
                ->Where([
                    ['patient_id','=', $patient->id],
                    ['medication_id' ,'=', $medication->id]
                ])->get()->first;
            $medPatientId = $medPatient->id->id;

  //Using Medication_patient_id to find MedTime       
            $assignedMedTimes = MedTime::where('med_patient_id' ,'=' , $medPatientId)->get();

//Filtering the Med Time according to the day 
            $yesterdayMedTimes = $assignedMedTimes->where('day', '=', 'yesterday')->all();
            $todayMedTimes = $assignedMedTimes->where('day', '=', 'today')->all();
            $tomorrowMedTimes = $assignedMedTimes->where('day', '=', 'tomorrow')->all();


            return view('medicationPatient.medTime', compact('patient','medication'),[
                'assignedMedTimes' => $assignedMedTimes,
                'yesterdayMedTimes' => $yesterdayMedTimes,
                'todayMedTimes' => $todayMedTimes,
                'tomorrowMedTimes' => $tomorrowMedTimes,
            ]);
        }

但是这仅在我获得 1 次药物治疗的时间时有效(分配给患者 X 的药物 X 时间),我如何设置循环或查询中的关系或 eloquent 获取我所有的用药时间 (患者 X 的 MED X,Y 时间)并将其传递给 blade。

很抱歉 post。如果你能给我看代码,我将不胜感激。

谢谢

你试过类似的东西吗?

public function patient()
{
  return $this->belongsToMany('App\Patient')->withPivot(['id']);
}

您可能会让自己更难直接转到该查询中的 table。如果是我,我可能会考虑稍微重构一下数据库,使这更容易,并轻松利用 Laravel 关系和枢轴。

我不确定您是否需要将数据存储在两个单独的 table 中。我希望尽可能规范化并将其压缩为单个 table。您似乎不需要在 Med_Time table 中重申自己 - med_patient table 添加了多个给定的药丸,因此它与 med_time table(我认为)。我建议只使用 medication_patient table 和支点:

id       medication_id          patient_id         Day      time      given
1         1 (MED X)             1 (Patient X)    Yesterday  0900        1
2         2 (MED y)             1 (Patient X)    Today      0900        0

您的关系将与您拥有的关系大致相同,但您可以直接从模型中绘制轴心。这是来自患者模型,但在你的问题中你们的关系对双方都很好

public function medication()
    {
        return $this->belongsToMany('App\Medication')->withPivot('Day', 'time', 'given');
    }

然后,当您需要访问数据时,只需拉动枢轴即可。示例:

$patient->pivot->Day... or $patient->pivot->time