【发布时间】:2014-12-12 21:06:38
【问题描述】:
我的控制器方法中有以下内容可将 JSON 返回到我的视图:
public function RolesForUser()
{
$userid = Input::get('userid');
$assigned = DB::table('assigned_roles')
->select(array('role_id'))
->where('user_id', '=', $userid)
->get();
$user = User::find($userid);
$roles = $user->roles();
$data = array('assigned' => $assigned, 'roles' => $roles);
return Response::json($data);
}
返回以下内容(使用 Fiddler 检查):
{"assigned":[{"role_id":"2"},{"role_id":"3"},{"role_id":"4"}],"roles":{}}
使用查询生成器的 SQL 语句返回正确的结果,但使用 Entrust 的方法(从 Entrust Issue 34 复制,在对我的用户模型进行更改后)不返回任何角色。
我也尝试了this SO question 中的解决方案,但它只是给了我一个 SQL 错误。
我在 Laravel 4.2.11 上出错的任何想法?
我的用户模型:
class User extends Eloquent implements UserInterface, RemindableInterface
{
use HasRole;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'Users';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('Password');
protected $primaryKey = 'UserID';
public $timestamps = false;
/*Standard methods removed for brevity*/
public function roles()
{
return $this->hasMany('Role');
}
}
【问题讨论】:
-
看起来用户模型和它的角色之间的关系是以一种不适合你的方式设置的。您可以附加使用的关系方法或特征吗?谢谢!
-
@hannesvdvreken - 我已经添加了我的用户模型的相关位