Answer the question
In order to leave comments, you need to log in
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'));
}
}
$consultations = Consultation::orWhere($patients->id);
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question