Better to just treat them as one type 'user'.
You can always use a map function to return a different view based on role if you must.
multiple tables is just a waste of time and energy
I'm building a dashboard system to control models. Some of these models have multiple types like User model. Let's say I have 4 types of users:
1- Doctor
2- Nurse
3- Patient
4- Front desk
All 4 types have a common table that contains common data like name, email, ..etc. Each type has a separate table like doctors, patient, ..etc.
The basic thing is that I would create 4 controllers (DoctorController, PatientController, ..) with almost the same functions only the fields, translations, and secondary tables are different.
I'm thinking of using an abstraction class to reduce the code redundancy like so:
namespace App\Http\Users;
abstract UserAbstract
{
public function index();
public function create();
public function store();
public function delete(User $user)
{
$user->delete();
}
}
The controllers:
namespace App\Http\Controllers;
class DoctorController extends Controller implements UserAbstract
{
public function index()
{
$doctors = User::query()
->where('user_type_id', 1)
->get():
return view('doctors.index', compact('doctors'));
}
}
Maybe add some properties there for the different names, tables, views ..etc to make it smaller.
I didn't test the above code so there code be some mistakes.
The second scenario is to create one controller and use the factory design pattern:
namespace App\Models\Quiz;
class UserFactory
{
public function create($type)
{
switch ($type) {
case 1:
return new Doctor();
case 2:
return new Nurse();
default:
throw new \InvalidArgumentException("Invalid user type: $type");
}
}
}
Controller:
namespace App\Http\Controllers;
class UserController extends Controller
{
protected $userFactory;
public function __construct(UserFactory $userFactory)
{
$this->userFactory= $userFactory;
}
public function create($userType, Request $request)
$user = $this->userFactory->create($userType);
$user->create($request);
}
}
Each user class:
namespace App\Http\Users;
Class Doctor
{
public function create($request)
{
$user = User::create($request);
$user->doctor()->create($request);
}
}
I'm trying to reduce the redundancy, save time, and optimize the code for the future but I don't want to make it over-engineered or complex.
Which way would you choose? Is there a better method?
Please or to participate in this conversation.