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);
// модель
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>