noblemfd
1 month ago
327
10
Laravel

How to perform complex user registration

Posted 1 month ago by noblemfd

Initially, I performed user registration using these tables users, roles, permissions, user_roles, role_permissions. This was successfully done using the codes below:

User model

protected $fillable = [
    'name', 
    'email', 
    'email_verified_at',
    'password', 
    'active', 
];

public function __construct(array $attributes = [])
{
    parent::__construct($attributes);
    self::created(function (User $user) {
        $registrationRole = config('panel.registration_default_role');

        if (!$user->roles()->get()->contains($registrationRole)) {
            $user->roles()->attach($registrationRole);
        }
    });
} 


public function roles()
{
    return $this->belongsToMany(Role::class);
}

User Controller

public function rules()
{
    return [
        'name'     => [
            'required',
        ],
        'email'    => [
            'required',
        ],
        'password' => [
            'required',
        ],
        'roles.*'  => [
            'integer',
        ],
        'roles'    => [
            'required',
            'array',
        ],
    ];
}

public function create()
{
    abort_unless(\Gate::allows('user_create'), 403);

    $roles = Role::all()->pluck('title', 'id');

    return view('admin.users.create', compact('roles'));
}

public function store(StoreUserRequest $request)
{
    abort_unless(\Gate::allows('user_create'), 403);

    $user = User::create($request->all());
    $user->roles()->sync($request->input('roles', []));

    return redirect()->route('admin.users.index');
}

This was successfully done.

However, I now have a new table called employees with the model class Employee. This have several foreign key keys.

Employee Model

protected $fillable = [
              'employee_code',
              'user_id',
          'line_manager_id',
              'address',
              'company_id',
              'email',
              'employee_designation_id',
              'employee_job_title_id',
              'employee_status_id',
              'employee_type_id',
              'employement_type_id',
              'employment_date',
              'first_name',
              'image',
              'last_name',
              'other_name',
              'phone',
              'resignation_date',
              'department_id',
              'date_of_birth',
              'title_id',
          ];

protected $casts = [];


public function user()
{
    return $this->belongsTo('App\User');
}

public function linemanager()
{
    return $this->belongsTo('App\User', 'line_manager_id');
}

public function company()
{
    return $this->belongsTo('App\Models\Organization\OrgCompany','company_id');
}

public function designation()
{
    return $this->belongsTo('App\Models\Hr\HrDesignation','employee_designation_id');
}

public function jobtitle()
{
    return $this->belongsTo('App\Models\Hr\HrJobTitle','employee_job_title_id');
}

public function employeestatus()
{
    return $this->belongsTo('App\Models\Hr\HrEmployeeStatus','employee_status_id');
}

public function employeetype()
{
    return $this->belongsTo('App\Models\Hr\HrEmployeeType','employee_type_id');
}

public function employementtype()
{
    return $this->belongsTo('App\Models\Hr\HrEmployementType','employement_type_id');
}

public function department()
{
    return $this->belongsTo('App\Models\Organization\OrgDepartment','department_id');
}

public function title()
{
    return $this->belongsTo('App\Models\Config\ConfigTitle','title_id');
}


public function rules()
{
    return [
        'employee_code'             => 'required|min:1|max:20|unique:hr_employees,employee_code,company_id',
        'first_name'                => 'required|min:3|max:50',
        'last_name'                 => 'required|min:3|max:50',
        'other_name'                => 'nullable|max:50',
        'image'                     => 'required|mimes:jpg,tiff,png,svg,gif,bmp,jpeg|max:10240',
        'address'                   => 'nullable|max:255',
        'email'                     => 'required|email|max:100|unique:hr_employees,email,company_id',   // official email
        'phone'                     => 'nullable|max:14|unique:hr_employees,phone',
        'phone2'                    => 'nullable|max:14|unique:hr_employees,phone2',
        'phone3'                    => 'nullable|max:14|unique:hr_employees,phone3',
        'phone4'                    => 'nullable|max:14|unique:hr_employees,phone4',
        'company_id'                => 'required',
        'gender_id'                 => 'required',
        'department_id'             => 'required',
    'title_id'              => 'required',
    'employement_type_id'       => 'required',
        'employee_type_id'          => 'required',
        'employee_designation_id'   => 'required',
    ];
}

In re-writing the user registration:

  1. How do I now write the controller and incorporate the employee model detail into the user registration.

  2. The email in users table should also be saved into the employee table while the company_id in employee table should be saved into user table.

  3. Which should be the main controller: employee or user

I'm somehow confused. Kindly show me examples.

Thank you

Please sign in or create an account to participate in this conversation.