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

时间:2019-05-18 17:01:59

标签: php laravel eloquent pivot-table laravel-query-builder

有人可以帮助我弄清楚这一点吗。首先,请不要重复CRUD and relation between three tables/Models in Laravel

Medication_Patient数据透视表和Med_Time有2个表

Medication_Patient数据透视表

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();

但是它没有提供我需要查找用药时间的数据透视表的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种服药时间(将Med X的时间分配给患者X的时间)时有效,如何在查询中设置循环或关系或雄辩地获取我所有的服药时间(患者X的MED X,Y时间),然后将其传递给刀片。

抱歉,很长的帖子。如果您能向我展示代码,将不胜感激。

谢谢

2 个答案:

答案 0 :(得分:1)

您可能不得不手动直接进入该查询中的表,这会使您的工作更加困难。如果是我,我可能会考虑重构数据库以简化此过程,并轻松利用Laravel关系和枢轴。

我不确定您是否需要将数据存储在两个单独的表中。我希望尽可能地规范化并将其折叠到一个表中。您似乎不需要在Lemma cartesian_inclusion U V A B C D : Included U A C /\ Included V B D -> Included (U * V) (Cartesian _ _ A B) (Cartesian _ _ C D). Proof. intros [HU HV] [x y] [HA HB]. split. - now apply HU. - now apply HV. Qed. 表中重复自己— Med_Time表添加了多个给定的药丸,因此它的作用与med_time表相同(我认为)。我建议仅使用一张具有枢轴的 medication_ Patient 表:

med_patient

您的关系将与您拥有的关系几乎相同,但是您可以从模型中直接绘制支点。这是来自患者模型的,但您的关系对您的问题都有利

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');
    }

答案 1 :(得分:0)

您尝试过类似的东西吗?

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