I have the following in my controller method that returns JSON to my view:
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);
}
Returns the following (inspected using Fiddler):
{"assigned":[{"role_id":"2"},{"role_id":"3"},{"role_id":"4"}],"roles":{}}
The SQL statement that uses Query Builder returns the correct results, but the method that uses Entrust (copied from Entrust Issue 34, after making the change to my User model) doesn't return any roles.
I also tried the solution in this SO question, but it just gives my an SQL error.
Any ideas where I'm going wrong, I'm on Laravel 4.2.11?
My User model:
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');
}
}