Php 拉威尔4号与MongoDB的关系
我使用Laravel4和MongoDB作为后端实现身份验证 我的用户模型如下:Php 拉威尔4号与MongoDB的关系,php,mongodb,authentication,laravel,Php,Mongodb,Authentication,Laravel,我使用Laravel4和MongoDB作为后端实现身份验证 我的用户模型如下: use Illuminate\Auth\UserInterface; /** * Class User * @property string $username * @property string $password */ class User extends Moloquent implements UserInterface { /** * Primary key for coll
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');
}
}
如您所见,我的用户只有用户名
和密码
,角色只有名称
问题是,如何保存相关模型?我已经尝试了很多方法,比如在Laravel eLoqont文档中,像$user->role()->save($role)
或者attach
而不是save
,但是只有$role->users()->save($user)
可以正常工作。为什么?不是所有的都应该工作吗?我似乎不太理解拉威尔的人际关系概念
另一件事是如何获得用户的角色?例如:
$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('adminP@%%');
$admin->save();
$role->users()->attach($admin);
}
}
}
我不确定为什么保存角色不起作用。也许您可以提供更多信息,但您也可以尝试
$user->role()->创建($role)。
但要确保$role是一个有说服力的对象
获取用户角色时,应使用$user->roles