joshmanhuwa
1 month ago
244
5
Laravel

Please help refactor this controller

Posted 1 month ago by joshmanhuwa

class UserController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $users = User::all(); return view('users.index', compact('users')); }

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
    return view ('users.create');
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    // Create User

    $check = User::where('email', $request->workEmail)->count();

    if ($check) {
        return "Aborted! User account already exists";
    }

    $user = new User;
    $user->name = $request->name . ' ' . $request->middleName . ' ' . $request->surname;
    $user->email = $request->workEmail;
    $user->password = Hash::make(strtolower($request->name));

    // return $request;

    $user->save();
    
    // Create profile
    $this->_createProfile($user, $request);

    return redirect('/users/' .  $user->id);
}

private function _createProfile(User $user, $request) {
    $profile = new Profile();

    $profile->user_id = $user->id;
    $profile->name = $request->name;
    $profile->middleName = $request->middleName;
    $profile->surname = $request->surname;

    $profile->gender = $request->gender;
    $profile->dateOfBirth = $request->dateOfBirth;
    $profile->nationality = $request->nationality;
    $profile->idNumber = $request->idNumber;
    $profile->race = $request->race;

    $profile->address1 = $request->address1;
    $profile->address2 = $request->address2;
    $profile->postalAddress1 = $request->postalAddress1;
    $profile->postalAddress2 = $request->postalAddress2;
    $profile->cellNumber = $request->cellNumber;
    $profile->homeNumber = $request->homeNumber;
    $profile->workExtention = $request->workExtention;
    $profile->workEmail = $user->email;


    $profile->position = $request->position;
    $profile->jobDesc = $request->jobDesc;
    $profile->contractType = $request->contractType;
    $profile->startDate = Carbon::parse($request->startDate);
    $profile->annualLeaveAllowance = $request->annualLeaveAllowance;
    $profile->annualSickAllowance = $request->annualSickAllowance;
    $profile->medicalProvider = $request->medicalProvider;
    $profile->pensionFundProvider = $request->pensionFundProvider;


    $profile->basicSalary = $request->basicSalary;
    $profile->mandateCommision = $request->mandateCommision;
    $profile->saleCommission = $request->saleCommission;
    $profile->phoneAllowance = $request->phoneAllowance;
    $profile->travelAllowance = $request->travelAllowance;
    $profile->employeeMedicalContribution = $request->employeeMedicalContribution;
    $profile->employerMedicalContribution = $request->employerMedicalContribution;
    $profile->employeePensionFundContribution = $request->employeePensionFundContribution;
    $profile->employerPensionFundContribution = $request->employerPensionFundContribution;
    $profile->houseAllowance = $request->houseAllowance;


    $profile->qualification1 = $request->qualification1;
    $profile->qualification2 = $request->qualification2;
    $profile->professionalAffiliation1 = $request->personalAffiliation1;
    $profile->professionalAffiliation2 = $request->personalAffiliation_2;
    $profile->hasDriversLicense = ($request->hasDriversLicense === "true");
    $profile->ownVehicle = ($request->ownVehicle === "yes");
    $profile->bankName = $request->bankName;
    $profile->bankAccountNumber = $request->bankAccountNumber;
    $profile->branchCode = $request->branchCode;
    // $profile->adminLevel;

    // files

    $profile->employementContract = $request->file('employementContract')->store('documents', 'public');
    $profile->identificationDocument = $request->file('identificationDocument')->store('documents', 'public');
    $profile->driversLicenseDocument = $request->file('driversLicenseDocument')->store('documents', 'public');
    $profile->curriculumVitae = $request->file('curriculumVitae')->store('documents', 'public');
    $profile->qualificationDocument1 = $request->file('qualificationDocument1')->store('documents', 'public');
    $profile->qualificationDocument2 = $request->file('qualificationDocument2')->store('documents', 'public');
    $profile->affiliationDocument1 = $request->file('affiliationDocument1')->store('documents', 'public');
    $profile->affiliationDocument2 = $request->file('affiliationDocument2')->store('documents', 'public');
    $profile->profileImage = $request->file('profileImage')->store('documents', 'public');

    $profile->save();

}

}

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