Created
January 6, 2016 16:55
-
-
Save rizqidjamaluddin/3d521bba5832b0e4074a to your computer and use it in GitHub Desktop.
This file contains 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 | |
// this is another kind of user. | |
class Admin extends Model { | |
protected $fillable = ['public_key']; | |
public function identity() { | |
return $this->morphMany(Identity::class, 'identifiable'); | |
} | |
} |
This file contains 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 | |
// this is what laravel calls a "user". When logging in, match against this. | |
// it also has identifiable_id and identifiable_type columns. | |
class Identity extends Model { | |
protected $fillable = ['email', 'password']; | |
public function user() { | |
return $this->morphTo(); | |
} | |
} |
This file contains 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 | |
// this is one kind of user. | |
class Member extends Model { | |
protected $fillable = ['name']; | |
public function identity() { | |
return $this->morphMany(Identity::class, 'identifiable'); | |
} | |
} |
This file contains 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 | |
class SomeController extends Controller { | |
// you can grab the user's "user" (i.e. Member or Admin) from Auth::user()->user. | |
// yea, kinda weird, change the wording if it seems ambiguous to you. | |
public function showUser(Identity $identity) { | |
if ($identity->user instanceof Admin) { | |
return View::make('admin.panel', $identity->user); | |
} else { | |
return View::make('user.panel', $identity->user); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Perfect example, thanks!