Skip to content

Commit

Permalink
add db transactions in RegisterController
Browse files Browse the repository at this point in the history
  • Loading branch information
arisurya7 committed Oct 12, 2022
1 parent ed6174b commit ddb146f
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions app/Http/Controllers/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
use App\Http\Requests\ApplicantRequest;
use App\Http\Requests\CompanyRequest;
use Illuminate\Support\Facades\Hash;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Applicant;
use App\Models\Company;
use Exception;

class RegisterController extends Controller
{
Expand All @@ -30,23 +31,36 @@ public function applicant(){
}

public function companyRegister(CompanyRequest $request){
$validData = $request->validated();
$validData['password'] = Hash::make($validData['password']);
$validData['photo'] = 'images/company/default.jpg';

Company::create($validData);

return redirect('/login')->with('success', 'Registration successfull! Please login.');
DB::beginTransaction();
try {
Applicant::factory()->create();
$validData = $request->validated();
$validData['password'] = Hash::make($validData['password']);
$validData['photo'] = 'images/company/default.jpg';
Company::create($validData);
DB::commit();
return redirect('/login')->with('success', 'Registration successfull! Please login.');
} catch (\Exception $e){
DB::rollBack();
throw $e;
}
}

public function applicantRegister(ApplicantRequest $request){

$validData = $request->validated();
$validData['password'] = Hash::make($validData['password']);
$validData['photo'] = 'images/applicant/default.jpg';
$validData['cv'] = '';
Applicant::create($validData);

return redirect('/login')->with('success', 'Registration successfull! Please login.');

DB::beginTransaction();
try {
$validData = $request->validated();
$validData['password'] = Hash::make($validData['password']);
$validData['photo'] = 'images/applicant/default.jpg';
$validData['cv'] = '';
Applicant::create($validData);
DB::commit();
return redirect('/login')->with('success', 'Registration successfull! Please login.');
} catch (Exception $e) {
DB::rollBack();
throw $e;
}
}
}

0 comments on commit ddb146f

Please sign in to comment.