K
K
Konstantin Malyarov2017-10-04 20:05:15
Laravel
Konstantin Malyarov, 2017-10-04 20:05:15

How to iterate over an array with previously obtained results?

I have a controller with page index.

public function index(Request $request)
    {
        if ($request->surname != null || $request->first_name != null || $request->second_name != null || $request->birthday != null || $request->day_consultation != null) {

            $patients = Patient::orWhere('surname', $request->surname)
                ->orWhere('first_name', $request->first_name)
                ->orWhere('second_name', $request->second_name)
                ->orWhere('birthday', $request->birthday)
                ->orderBy('surname')
                ->orderBy('first_name')
                ->orderBy('second_name')
                ->orderBy('birthday');
            $consultations = Consultation::union($patients->id);
            return view('administration.protocol.index', compact('consultations'));
        } else {
            $consultations = Consultation::orderBy('day_consultation')
                ->get();
            return view('administration.protocol.index', compact('consultations'));
        }
    }

Through the GET request, we get the variables for the search: first, it looks in the patients table, and receives an array. Then iterate over the $patients array and get all $consultations.
That is, I can get an array of patients, but I can’t link a consultation to each patient.
Is it possible to pass an array to the request?
$consultations = Consultation::orWhere($patients->id);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
anlamas, 2017-10-08
@anlamas

How to build relationships

// модель 
class Patient extends Model
{
   public function consultations()
    {
        return $this->hasMany(Consultation::class());
    }
}

// контроллер
public function index(Request $request)
    {
        if ($request->surname != null || $request->first_name != null || $request->second_name != null || $request->birthday != null || $request->day_consultation != null) {

            $patients = Patient::with('consultations')->orWhere('surname', $request->surname)
                ->orWhere('first_name', $request->first_name)
                ->orWhere('second_name', $request->second_name)
                ->orWhere('birthday', $request->birthday)
                ->orderBy('surname')
                ->orderBy('first_name')
                ->orderBy('second_name')
                ->orderBy('birthday')
                ->get(); 
            return view('administration.protocol.index', compact('patients'));
        } else {
            $consultations = Consultation::orderBy('day_consultation')
                ->get();
            return view('administration.protocol.index', compact('consultations'));
        }
    }

// вывод в blade.php
<ul>
@forelse($patients as $patient)
    <li>Пациенту {{ $patient->name }}
    <ul>
    @forelse($patient->consultations as $consultation)
        <li>назначено на {{ $consultation->created_at }}</li>
    @empty
        <li>Нет назначенных консультаций</li>
    @endforelse
    </ul>
    </li>
@empty
    <li>Нет записей</li>
@endforelse
</ul>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question