Laravel. How to properly implement the registration of multiple types of users with completely different fields?

Hi everybody!

Work on the portal "job search", something like hh.ru. At the moment I have implemented a registration/authorization using polimorfnykh ties, here is a small example:
- the essence of the User (User) stores fields only: email and password, has a connection
public function profile()
{
 return $this->morphTo();
 }
this is useful when the authorization is sufficient to have one point of entry and the system will give the user regardless of its type;
- added two entities to the Applicant (Jobseeker) and the Employer (Employer), have the link
public function user()
{
 return $this->morphOne('App\User', 'profile');
 }


At first I thought this approach was acceptable, but the farther into the forest, the more slojnostei ...
The code I'm trying to get user I have to call in the following way
Auth::user()->profile
// Validate user 
Auth::user()->profile->isEmployer()
// The company may update Manager (recruiter), 
// for this entity Employer update() method are scratching their heads that it is better to pass User id and get it again 
$user = User::findOrFail($id); 
$user->profile()->update(...) 
//id or Employer and 
$employer->findOrFail($id); 
$employer->update(...)

// and the sore place is to create and update user
// for me this piece of code looks horrible 
// side effect polymorphic relations
$employer = Employer::create([
 'phone' => $request->get('phone'),
 'full_name' => $request->get('full_name'),
 'company_id' => Auth::user()->profile->company_id
]);

$employer->user()->create([
 'email' => $request->get('email'),
 'password' => $request->get('password')
]);

Then I found another implementation by adding a new guard in the auth config, but with this approach I kadetsa will have to duplicate the controllers authorization, registration, etc ...

I should be grateful for the good advice, thank you!
April 4th 20 at 00:27
1 answer
April 4th 20 at 00:29
Can be implemented using roles for example.
Combining all fields into one table, putting nullable()->default(null)
And in your code just check through Gate.
I thought about this realization, but what to do when discrepancies in 10-15 fields, then the User table will be obscenely large (greater than 30 lines of code).

Thank you Yuri for the tip, will need groshenko to weigh the pros and cons. - Duncan.Ward49 commented on April 4th 20 at 00:32

Find more questions by tags Laravel