Skip to content

Instantly share code, notes, and snippets.

@rizqidjamaluddin
Created January 6, 2016 16:55
Show Gist options
  • Save rizqidjamaluddin/3d521bba5832b0e4074a to your computer and use it in GitHub Desktop.
Save rizqidjamaluddin/3d521bba5832b0e4074a to your computer and use it in GitHub Desktop.
<?php
// this is another kind of user.
class Admin extends Model {
protected $fillable = ['public_key'];
public function identity() {
return $this->morphMany(Identity::class, 'identifiable');
}
}
<?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();
}
}
<?php
// this is one kind of user.
class Member extends Model {
protected $fillable = ['name'];
public function identity() {
return $this->morphMany(Identity::class, 'identifiable');
}
}
<?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);
}
}
}
@JustSteveKing
Copy link

Perfect example, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment