如何从Laravel中的数组获取特定的数组值

时间:2019-06-08 14:03:57

标签: php arrays laravel

我想从我从此代码获得的数组中获取特定的数组数据:

$user       = $request->instance()->query('user');
var_dump($user);exit;

结果是:

  

array(1){[“ user”] => object(App \ Models \ User)#332(27){   [“ fillable”:protected] => array(5){[0] => string(4)“ name” [1] =>   字符串(8)“用户名” [2] =>字符串(5)“电子邮件” [3] =>字符串(8)   “密码” [4] =>字符串(5)“令牌”} [“隐藏”:受保护的] => array(2){   [0] =>字符串(8)“密码” [1] =>字符串(14)“ remember_token”}   [“ casts”:受保护的] => array(1){[“” email_verified_at“] =>字符串(8)   “ datetime”} [“连接”:受保护] => string(5)“ mysql”   [“ table”:protected] =>字符串(5)“ users” [“ primaryKey”:protected] =>   string(2)“ id” [“ keyType”:protected] => string(3)“ int”   [“ incrementing”] => bool(true)[“ with”:protected] => array(0){}   [“ withCount”:protected] => array(0){} [“ perPage”:protected] => int(15)   [“ exists”] => bool(true)[“ wasRecentlyCreated”] => bool(false)   [“属性”:受保护] =>数组(15){[“” id“] => int(3)[”名称“] =>   string(14)“ Manchesteriyah” [“用户名”] => string(14)“ manchesteriyah”   [“电子邮件”] =>字符串(24)“ manchesteriyah@gmail.com” [“ facebook”] =>   string(37)“ https://web.facebook.com/xkionna” [“电话”] => NULL   [“ email_verified_at”] => NULL [“ password”] =>字符串(60)   “ $ 2y $ 10 $ IrqHE1JfyH0bJ0XD / Hjy.efLg95y / buTIir0PuXcOqDb1yCSS69Oe”   [“ image”] => NULL [“ description”] => NULL [“ role”] => string(1)“ 1”   [“ token”] =>字符串(20)“ ymsJxEtFnxdPBWYwlYFw” [“ member_expiration”] =>   字符串(19)“ 2019-07-08 20:33:29” [“ created_at”] =>字符串(19)   “ 2019-06-08 20:30:25” [“ updated_at”] =>字符串(19)“ 2019-06-08   20:33:29“} [”原始“:受保护] => array(15){[” id“] => int(3)   [“ name”] =>字符串(14)“ Manchesteriyah” [“ username”] =>字符串(14)   “ manchesteriyah” [“电子邮件”] =>字符串(24)“ manchesteriyah@gmail.com”   [“ facebook”] =>字符串(37)“ https://web.facebook.com/xkionna”   [“电话”] => NULL [“ email_verified_at”] => NULL [“密码”] =>   弦(60)   “ $ 2y $ 10 $ IrqHE1JfyH0bJ0XD / Hjy.efLg95y / buTIir0PuXcOqDb1yCSS69Oe”   [“ image”] => NULL [“ description”] => NULL [“ role”] => string(1)“ 1”   [“ token”] =>字符串(20)“ ymsJxEtFnxdPBWYwlYFw” [“ member_expiration”] =>   字符串(19)“ 2019-07-08 20:33:29” [“ created_at”] =>字符串(19)   “ 2019-06-08 20:30:25” [“ updated_at”] =>字符串(19)“ 2019-06-08   20:33:29“} [”更改“:受保护] => array(0){} [”日期“:受保护] =>   array(0){} [“ dateFormat”:受保护的] => NULL [“ appends”:受保护的] =>   array(0){} [“ dispatchesEvents”:protected] => array(0){}   [“ observables”:受保护的] => array(0){} [“ relations”:受保护的] =>   array(0){} [“ touches”:受保护] => array(0){} [“ timestamps”] =>   bool(true)[“ visible”:protected] => array(0){}   [“ guarded”:protected] => array(1){[0] => string(1)“ *”}   [“ rememberTokenName”:protected] =>字符串(14)“ remember_token”}}

我想要得到的是用户名值,我已经尝试过类似的解决方案:

$user->username

但是它给我这样的错误:

  

试图获取非对象的属性“用户名”

如何解决此问题?感谢您的关注。

1 个答案:

答案 0 :(得分:1)

$user变量似乎持有一个包含1个元素的数组,而不是User本身。您必须首先从数组中检索User对象,然后访问 username 属性。

$user = $request->instance()->query('user')['user'];
var_dump($user->username); exit;