In the meantime I'm using this method on my repository, I'm not a big fan of nested foreach though.
/**
* Return all Consultations for the User
*
* @param User $user
* @return \Illuminate\Support\Collection
*/
public function getAllForUser(User $user)
{
$patients = Patient::with(['consultations'])->whereUserID($user->id)->get();
$consultations = new Collection;
foreach ($patients as $patient)
{
foreach ($patient->consultations as $index => $consultation)
{
$consultation->number = count($patient->consultations) - $index;
$consultation->patient = $patient;
$consultations->add($consultation);
}
}
return $consultations->sortBy('date_system', null, true);
}
I knew I was forgetting something. Thanks! :)
The solution if someone is wondering thanks to @pmall would be:
/**
* Return all Consultations for the User
*
* @param User $user
* @return \Illuminate\Support\Collection
*/
public function getAllForUser(User $user)
{
return $user->consultations()->with('patient')->orderBy('date', 'desc')->get();
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community