2014-06-06 75 views
0

我正在使用Laravel 4和MongoDB實現身份驗證,並使用https://github.com/jenssegers/Laravel-MongoDB作爲後端。Laravel 4與MongoDB的關係

我的用戶模型是這樣的:

use Illuminate\Auth\UserInterface; 

/** 
* Class User 
* @property string $username 
* @property string $password 
*/ 
class User extends Moloquent implements UserInterface { 

    /** 
    * Primary key for collection 
    * 
    * @var string 
    */ 
    protected $primaryKey = 'username'; 

    /** 
    * The attributes excluded from the model's JSON form. 
    * 
    * @var array 
    */ 
    protected $hidden = array('password'); 

    /** 
    * Relationship with role 
    * 
    * @return \Illuminate\Database\Eloquent\Relations\HasOne 
    */ 
    public function role() { 
     return $this->belongsTo('Role'); 
    } 

    /** 
    * Get the unique identifier for the user. 
    * 
    * @return mixed 
    */ 
    public function getAuthIdentifier() 
    { 
     return $this->username; 
    } 

    /** 
    * Get the password for the user. 
    * 
    * @return string 
    */ 
    public function getAuthPassword() 
    { 
     return $this->password; 
    } 

    /** 
    * Get the token value for the "remember me" session. 
    * 
    * @return string 
    */ 
    public function getRememberToken() 
    { 
     return $this->remember_token; 
    } 

    /** 
    * Set the token value for the "remember me" session. 
    * 
    * @param string $value 
    * @return void 
    */ 
    public function setRememberToken($value) 
    { 
     $this->remember_token = $value; 
    } 

    /** 
    * Get the column name for the "remember me" token. 
    * 
    * @return string 
    */ 
    public function getRememberTokenName() 
    { 
     return 'remember_token'; 
    } 

} 

,這是我的榜樣:

/** 
* Class Role 
* @property string name 
*/ 
class Role extends Moloquent { 

    /** 
    * Primary key for collection 
    * 
    * @var string 
    */ 
    protected $primaryKey = 'name'; 

    /** 
    * Collection name for role 
    * 
    * @var string 
    */ 
    protected $collection = 'roles'; 

    /** 
    * Relationship with users 
    * 
    * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany 
    */ 
    public function users() { 
     return $this->belongsToMany('User'); 
    } 
} 

正如你看到的,我的用戶只能usernamepassword和角色只有name

問題是,如何保存相關模型?我嘗試了很多方法,如在Laravel Eloqoent文檔中,如$user->role()->save($role)attach而不是save,但只有$role->users()->save($user)正常工作。爲什麼?它們不應該全部工作嗎?我似乎不瞭解Laravel的關係概念非常好。

另一件事是如何獲得用戶的角色?例如:

$user = Auth::user(); 
var_dump($user->role()->get()); 

給出一個空的集合。這不應該給我用戶的角色?

我很困惑!

預先感謝您。

編輯:這裏是我的接種:將

class RoleTableSeeder extends Seeder { 

    public function run() { 
     $adminRole = Role::where('name', 'admin')->first(); 
     if(empty($adminRole)) { 
      Role::create(array(
       'name' => 'admin', 
      )); 
      Role::create(array(
       'name' => 'superuser', 
      )); 
      Role::create(array(
       'name' => 'user', 
      )); 
     } 
    } 
} 


class UserTableSeeder extends Seeder { 

    public function run() { 
     $admin = User::where('name', 'admin')->first(); 
     if(empty($admin)) { 
      $role = Role::where('name', 'admin')->first(); 

      $admin = new User(); 
      $admin->username = 'admin'; 
      $admin->password = Hash::make('[email protected]%%'); 
      $admin->save(); 

      $role->users()->attach($admin); 
     } 
    } 
} 

回答

0

IM不知道爲什麼儲蓄的作用是不工作,也許你可以給更多的信息,但你也可以嘗試$user->role()->create($role).

但要確保$角色是一個雄辯的對象。

獲取用戶角色時,應該使用$user->roles