Last active
October 6, 2020 00:58
-
-
Save deframe/9859da927954b5d1a1c7 to your computer and use it in GitHub Desktop.
Authentication -> User polymorphic relationships in Laravel.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Schema::create('users', function(Blueprint $table) | |
{ | |
$table->increments('id'); | |
$table->string('username')->unique(); | |
$table->string('password'); | |
$table->unsignedInteger('user_typeable_id'); | |
$table->string('user_typeable_type'); | |
$table->rememberToken(); | |
}); | |
DB::table('users')->insert([ | |
'username' => 'jbloggs', | |
'password' => Hash::make('password'), | |
'user_typeable_id' => 1, | |
'user_typeable_type' => 'App\Student' | |
]); | |
DB::table('users')->insert([ | |
'username' => 'jdoe', | |
'password' => Hash::make('password'), | |
'user_typeable_id' => 1, | |
'user_typeable_type' => 'App\Teacher' | |
]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Http\Controllers; | |
use App\Http\Requests\LoginRequest; | |
class AuthController extends Controller { | |
/** | |
* @param \App\Http\Requests\LoginRequest $request | |
* @return \Illuminate\Http\RedirectResponse | |
*/ | |
public function submitLogin(LoginRequest $request) | |
{ | |
$authenticated = app('auth')->attempt( | |
$request->only('email', 'password'), | |
$request->has('remember') | |
); | |
if ($authenticated) | |
{ | |
$user = app('auth')->user()->userTypeable; | |
if (is_a($user, 'App\Student')) | |
{ | |
// Student has logged in... | |
} | |
else if (is_a($user, 'App\Teacher')) | |
{ | |
// Teacher has logged in... | |
} | |
return redirect()->intended(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Schema::create('students', function(Blueprint $table) | |
{ | |
$table->increments('id'); | |
$table->string('forename'); | |
$table->string('surname'); | |
$table->string('email'); | |
$table->string('student_number'); | |
$table->boolean('enrolled'); | |
}); | |
DB::table('students')->insert([ | |
'id' => 1, | |
'forename' => 'Joe', | |
'surname' => 'Bloggs', | |
'email' => '[email protected]', | |
'student_number' => 'QWE12345', | |
'enrolled' => true | |
]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Schema::create('teacher', function(Blueprint $table) | |
{ | |
$table->increments('id'); | |
$table->string('forename'); | |
$table->string('surname'); | |
$table->string('email'); | |
$table->date('contract_start_date'); | |
$table->string('subject'); | |
}); | |
DB::table('teachers')->insert([ | |
'id' => 1, | |
'forename' => 'John', | |
'surname' => 'Doe', | |
'email' => '[email protected]', | |
'contract_start_date' => new DateTime(), | |
'subject' => 'Mathematics' | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment