Q:

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

Could Some one help me to get figure this out.To start of this is not a duplicate of CRUD and relation between three tables/Models in Laravel.
There are 2 tables Medication_Patient Pivot Table and Med_Time
Medication_Patient Pivot 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)

And MEdTime which store time and where the medication was given or not
  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 

On the Models I have 
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');
    }
}

To get the Medication's assign to a Patient 
 $assignedMeds = $patient->medication()->get();

but it doesn't give me the ID of Pivot table which I need to find the Time for medication, So I used (PLEASE LET ME KNOW IF THERE IS A BETTER WAY TO DO THIS)
//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,
            ]);
        }

But this only works when I am getting the Time for 1 Medication (Med X time assigned to Patient X), how do I setup a loop or relation in query or eloquent to get me all the medication time (MED X,Y time for Patient X) and pass it into blade.
Sorry of the long post. Would Appreciate if you could show me the Code. 
Thank you 

A:

You may be making it a little harder on yourself having to go directly to the table in that query.  If it were me, I might consider refactoring the database a little bit to make this easier, and to easily take advantage of the Laravel relationships & pivots.
I'm not sure you need to store the data in two separate tables.  I would look to normalize as much as possible and collapse this down to a single table.  You don't seem to need to reiterate yourself in the Med_Time table -- the med_patient table adds multiple pill's given, so it serves the same purpose as the med_time table (I think).  I suggest just a medication_patient table with the pivots:
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

Your relations will be much the same as you have them, but you can draw the pivots right from the model.  This is from the Patient model, but your relations are good for both in your question
public function medication()
    {
        return $this->belongsToMany('App\Medication')->withPivot('Day', 'time', 'given');
    }

Then, when you need to access the data, just pull the pivot.  Example:
$patient->pivot->Day... or $patient->pivot->time

