This is not a good approach. Just use the users table with an account_type field or something to differentiate the type of user (or use roles, etc). You can use all of the same fields and have them all in the user table but just set the ones that are not common to both account types to nullable, so a company wouldn't store the school, etc.
After registration, users are redirected to the login page. Help me debug?
I'm not really sure what's tripping my code up. I've bootstrapped my authentication with php artisan make:auth, and then customized the RegisterController to fit my multi-model setup.
Let's say my app has two models: Student and Company that both extend the User model. I'd like my app to login both of these models. For now, as the title states, I'm able to register new Students, but wasn't able to log them in. My RegisterController (stripped of comments):
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Validation\Rule;
use Illuminate\Http\Request;
use App\Student;
use App\Company;
class RegisterController extends Controller
{
use RegistersUsers;
protected $redirectTo = '/home';
public function __construct()
{
$this->middleware('guest');
}
public function register(Request $request)
{
$this->validator($request->all())->validate();
$guard = $request->_account_type;
$user = $this->create($request->all());
auth()->guard($guard)->login($user);
dd(auth()->user()); // returns null.
return redirect($this->redirectPath());
}
protected function validator(array $data)
{
if ($data['_account_type'] == 'student') {
return Validator::make($data, [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:students',
'password' => 'required|string|min:6|confirmed',
'school' => 'required|string',
'course' => [
'required',
Rule::in(app()->get('courses')),
],
'address' => 'required|string|min:10',
'contact_number' => 'required|min:7',
]);
}
if ($data['_account_type'] == 'company') {
return Validator::make($data, [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:companies',
'password' => 'required|string|min:6|confirmed',
'school' => 'required|string',
'field' => [
'required',
Rule::in(app()->get('courses')),
],
'address' => 'required|string|min:10',
'contact_number' => 'required|min:7',
]);
}
return back()->withErrors('Invalid account type.');
}
protected function create(array $data)
{
if ($data['_account_type'] == 'student') {
return Student::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'school' => $data['school'],
'course' => $data['course'],
'address' => $data['address'],
'contact_number' => $data['contact_number'],
]);
}
if ($data['_account_type'] == 'company') {
return Company::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
}
}
Note that in the register method, a call to dd(auth()->user()); returns null even after the attempt to log the student in with auth()->guard($guard)->login($user);.
Any hints? Or maybe I should provide more info?
Please or to participate in this conversation.