Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

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);
}
Last updated 2 years ago.
0

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();
}
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

deivian deivian Joined 6 Jul 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.