我正在尝试获取role_id,但我不知道该怎么做,因为它不起作用:
Auth :: user-> roles()-> role_id
object(Illuminate \ Database \ Eloquent \ Collection)#843(1)
{[“ items”:protected] => array(1){[0] => object(App \ Role)#839(23){[“ table”:protected] => string(5)“ roles “ [[” connection“:protected] => NULL [” primaryKey“:protected] => string(2)” id“ [” perPage“:protected] => int(15)[” incrementing“] => bool(true )[“ timestamps”] => bool(true)[“ attributes”:protected] => array(3){[“” id“] => string(1)” 6“ [” role_title“] => string(11 )“ FaceInicial” [“ role_slug”] =>字符串(11)“ faceinicial”} [“原始”:受保护的] => array(5){[“ id”] =>字符串(1)“ 6” [“ role_title “] =>字符串(11)” FaceInicial“ [” role_slug“] =>字符串(11)” faceinicial“ [” pivot_user_id“] =>字符串(2)” 15“ [” pivot_role_id“] =>字符串(1) “ 6”} [“关系”:受保护] => array(1){[“”数据透视“] =>对象(Illuminate \ Database \ Eloquent \ Relations \ Pivot)#841(26){[”“父母”:受保护] => object(App \ User)#817(24){[“表格”:受保护] =>字符串(5)“用户” [“可填充”:受保护] => array(4){[0] =>字符串(4)“名称” [1] =>字符串(5)“电子邮件” [2] =>字符串(8)“密码” [3] =>字符串(8)“用户名”} [“隐藏”:受保护的] => array(2){[0] =>字符串(8)“密码” [1] =>字符串(14)“ remember_token”} [“连接”:受保护的] => NULL [“ primaryKey”:保护的] =>字符串(2)“ id” [“ perPage”:protected] => int(15)[“ incrementing”] => bool(true)[“ timestamps”] => bool(true)[“ attributes”:protected] => array(9){[“ id”] =>字符串(2)“ 15” [“ name”] =>字符串(13)“ administrador” [“ email”] =>字符串(13)“ test@test.com “ [”“ password”] =>字符串(60)“ $ 2y $ 10 $ mC / LOcYzv3akuC1nFYawdOEzDINJq9pyVl8Ej4vY1XlcWfnODbkQy” [“ remember_token”] =>字符串(60)“ o2PV91bkFAAHYUjgy9YdYY(Y {1} -15 11:27:42“ [” updated_at“] => string(19)” 2019-02-11 13:13:49“ [” username“] => string(13)” administrador“ [” deleted_at“] => NULL} [“原始”:受保护] => array(9){[“ id”] =>字符串(2)“ 15” [“名称”] =>字符串(13)“管理员” [“电子邮件” ] =>字符串(13)“ test@test.com” [“密码”] =>字符串(60)“ $ 2y $ 10 $ mC / LOcYzv3akuC1nFYawdOEzDINJq9pyVl8Ej4vY1XlcWfnODbkQy” [“ remember_token”] =>字符串(60)“ o2PV91 Ujgy9Yd8ZIRtXgdrapByVLgeuWklcaKbSL0QAXtIvY4PQ6C“ [” created_at“] =>字符串(19)” 2018-10-15 11:27:42“ [” updated_at“] =>字符串(19)” 2019-02-11 13:13:49“ [”用户名“] =>字符串(13)” administrador“ [” deleted_at“] =>空} [”关系“:受保护] => array(0){} [”可见“:受保护] => array(0){ } [“ appends”:protected] => array(0){} [“ guarded”:protected] => array(1){[0] => string(1)“ ”}} [“ dates”: protected] => array(0){} [“ dateFormat”:protected] => NULL [“ casts”:protected] => array(0){} [“ touches”:protected] => array(0){} [“ observables”:受保护] => array(0){} [“ with”:受保护] => array(0){} [“ morphClass”:protected] => NULL [“ exists”] => bool(true )[“ wasRecentlyCreated”] => bool(false)[“ forceDeleting”:protected] => bool(false)} [“ foreignKey”:protected] => string(7)“ user_id” [“ otherKey”:protected] = > string(7)“ role_id” [“ guarded”:受保护的] => array(0){} [“ connection”:protected] => NULL [“ table”:protected] => string(9)“ role_user” [ “ primaryKey”:受保护] =>字符串(2)“ id” [“ perPage”:pro tected] => int(15)[“ incrementing”] => bool(true)[“ timestamps”] => bool(false)[“ attributes”:protected] => array(2){[“” user_id“] = > string(2)“ 15” [“ role_id”] => string(1)“ 6”} [“原始”:受保护] => array(2){[“” user_id“] => string(2)” 15 “ [[” role_id“] =>字符串(1)” 6“} [”关系“:受保护] => array(0){} [”隐藏“:受保护] => array(0){} [”可见“ :protected] => array(0){} [“ appends”:protected] => array(0){} [“ fillable”:protected] => array(0){} [“ dates”:protected] => array(0){} [“ dateFormat”:protected] => NULL [“ casts”:protected] => array(0){} [“ touches”:protected] => array(0){} [“ observables” :protected] => array(0){} [“ with”:protected] => array(0){} [“ morphClass”:protected] => NULL [“ exists”] => bool(true)[“ wasRecentlyCreated “] => bool(false)}} [”隐藏“:受保护] =>数组(0){} [”可见“:受保护] => array(0){} [”追加“:受保护] =>数组(0){} [“ fillable”:受保护] => array(0){} [“ guarded”:受保护] => array(1){[0] =>字符串(1)“ ”} [“日期”:受保护] => ar ray(0){} [“ dateFormat”:protected] => NULL [“ casts”:protected] => array(0){} [“ touches”:protected] => array(0){} [“ observables” :protected] => array(0){} [“ with”:protected] => array(0){} [“ morphClass”:protected] => NULL [“ exists”] => bool(true)[“ wasRecentlyCreated “] => bool(false)}}
答案 0 :(得分:2)
您应该使用以下解决方案。
$user = \Auth::user();
$name=$user->name;
$roles = $user->getRoles();
OR
您可以尝试以下方法:
Auth::user()->roles[0]->id
答案 1 :(得分:1)
据我了解,这应该返回您想要的内容
Auth::user()->roles()->pluck('id')
(或role_id
,很难读懂上面的代码块)
如果有,则可以在您的用户模型中编写
public function getRoles() {
return $this->roles()->pluck('id')
}
然后
Auth::user()->getRoles();
答案 2 :(得分:0)
您的日志消息无法阅读,但我发现您的代码有什么问题。
您无法从数组中获取属性,只能从单个集合中获取属性,因此必须选择所需的集合,在我的示例中,我选择了first()
一个(使用laravel特殊方法)。 / p>
Auth::user->roles()->first()->role_id;
或者您可以使用普通的php方式
Auth::user->roles[0]['role_id'];
注意:在第二个示例中,您必须使用roles
,不能使用roles()
,因为它将返回Relations
(在您的情况下为BelongsToMany
)类,而不是collection