Skip to content

Instantly share code, notes, and snippets.

@nikhilj1
Last active June 26, 2017 13:37
Show Gist options
  • Select an option

  • Save nikhilj1/9bb6668ecbce19aa38dbf2265f2e1c6e to your computer and use it in GitHub Desktop.

Select an option

Save nikhilj1/9bb6668ecbce19aa38dbf2265f2e1c6e to your computer and use it in GitHub Desktop.
Polymorphic relations
<?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();
});
}
<?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');
}
}
<?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