Last active
June 26, 2017 13:37
-
-
Save nikhilj1/9bb6668ecbce19aa38dbf2265f2e1c6e to your computer and use it in GitHub Desktop.
Polymorphic relations
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 | |
| public function up() | |
| { | |
| \Schema::create('users', function (Blueprint $table) { | |
| $table->increments('id'); | |
| $table->string('name'); | |
| $table->tinyInteger('role_id')->default(0); | |
| $table->string('role_type')->default('asdsa'); | |
| $table->timestamps(); | |
| }); | |
| Schema::create('userType1', function (Blueprint $table) { | |
| $table->increments('id'); | |
| $table->timestamps(); | |
| }); | |
| } |
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; | |
| class User extends Model | |
| { | |
| public function role() | |
| { | |
| return $this->morphTo(); | |
| } | |
| } | |
| class UserType1 extends Model | |
| { | |
| public function users() | |
| { | |
| return $this->morphMany('App\User', 'role'); | |
| } | |
| } |
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 | |
| public function run() | |
| { | |
| // | |
| $usertype = new \App\UserType(); | |
| $usertype->save(); | |
| // Create Usertype Fisrt So that it can be assigned to user | |
| $user = new \App\User(); | |
| $user->name= 'user1'; | |
| $usertype->users()->save($user); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment