I'm making a shared controller for dentists and patients, to show their appointments.
I made a repo to keep the controller clean and added the following methods to begin.
I'm wondering, is this good practice? The right way to go about this? Looking forward to your ideas!
class AppointmentRepository
{
private $appointment;
public function __construct(Appointment $appointment)
{
$this->appointment = $appointment;
}
public function getAll()
{
return $this->appointment->all();
}
public function getAllByDentistId($id)
{
return $this->appointment->where('dentist_id', $id)->get();
}
public function getAllByPatientId($id)
{
return $this->appointment->where('patient_id', $id)->get();
}
public function getOneByDentistId($dentistId, $appointmentId)
{
return $this->appointment->where('dentist_id', $dentistId)->where('id', $appointmentId)->first();
}
public function getOneByPatientId($patientId, $appointmentId)
{
return $this->appointment->where('patient_id', $patientId)->where('id', $appointmentId)->first();
}
public function getConfirmedByPatientId($id)
{
return $this->appointment->where('patient_id', $id)->confirmed()->get();
}
public function getConfirmedByDentistId($id)
{
return $this->appointment->where('dentist_id', $id)->confirmed()->get();
}
}
For example instead of getAllByDentistId, just do $dentist->appointments.
I usually advice against repository pattern. What if you need more conditions for getConfirmedByDentistId, you'll end up with getConfirmedAndSomethingByDentistId and soon it will become unmanageable. With eloquent you just : $dentist->appointments()->confirmed()->something()->get();. More flexible and elegant.
@narco what you wrote above is basically wrapping eloquent in methods for the sake of wrapping it in method. What the point of using findAllBy when you can directly use $model->get().
When you have the same big query used in many places, you can wrap it into a class and thats it.