php - Q: How to create multi auth user profiles in Laravel 7
Get the solution ↓↓↓I am working on a new project, and so far I have managed to get multi auth working. But now I am trying to find a way to create and associate a Profile for both type of auth in my case Applicants and Employers right from the RegisterController.
It might be simple for some of you but all new to PHP and laravel.
auth.php
'guards' => [
[...]
'applicant' => [
'driver' => 'session',
'provider' => 'applicants',
],
],
'providers' => [
[...]
'applicants' => [
'driver' => 'eloquent',
'model' => App\Models\Applicant::class,
],
],
registerController.php
public function showApplicantRegisterForm()
{
return view('auth.register', ['url' => 'applicant']);
}
protected function createApplicant(Request $request)
{
$this->validator($request->all())->validate();
$applicant = Applicant::create([
'name' => $request['name'],
'email' => $request['email'],
'password' => Hash::make($request['password']),
]);
return redirect()->intended('login/applicant');
}
routes.php
Route::get('/login/applicant', 'Auth\LoginController@showApplicantLoginForm');
Route::get('/register/applicant', 'Auth\RegisterController@showApplicantRegisterForm');
Route::post('/login/applicant', 'Auth\LoginController@applicantLogin');
Route::post('/register/applicant', 'Auth\RegisterController@createApplicant');
Route::view('/applicant', 'applicant');
Answer
Solution:
Finally got it working, providingApplicantsProfile
fields inauth/registerController
and associate it to an applicant.
Instead of using one register view, I have added a custom register view for applicants. I'm sure there's another work around to resolve duplicate code using conditions in one view. but so far this is what's working for me.
public function showApplicantRegisterForm()
{
return view('auth.applicant_register', ['url' => 'applicant']);
}
protected function createApplicant(Request $request)
{
$this->validator($request->all())->validate();
$applicant = Applicant::create([
'name' => $request['name'],
'email' => $request['email'],
'password' => Hash::make($request['password']),
]);
ApplicantsProfile::create([
'applicant_id' => $applicant->id,
'job_title' => $request['job_title'],
]);
return redirect()->intended('login/applicant');
}
applicant_register.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ isset($url) ? ucwords($url) : ""}} {{ __('Register') }}</div>
<div class="card-body">
@isset($url)
<form method="POST" action='{{ url("register/$url") }}' aria-label="{{ __('Register') }}">
@else
<form method="POST" action="{{ route('register') }}" aria-label="{{ __('Register') }}">
@endisset
@csrf
[...]
<div class="form-group row">
<label for="job_title" class="col-md-4 col-form-label text-md-right">{{ __('JobTitle') }}</label>
<div class="col-md-6">
<input id="job_title" type="text" class="form-control @error('job_title') is-invalid @enderror" name="job_title" value="{{ old('job_title') }}" required autocomplete="job_title" autofocus>
@error('job_title')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
[...]
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: apache/2.4.52 (win64) openssl/1.1.1m php/8.1.2 server at localhost port 80
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.