无法从Eloquent Collection访问所需的属性

时间:2017-01-25 03:12:00

标签: php database orm eloquent

$result = User::where('email_add', '=', $username)->first();

这是我在$ result

上使用var_dump时出现的内容
  

对象(用户)#22(30){[" user_id"] => int(0)[" fname"] => string(0)"" [" L-NAME"] => string(0)"" [" MNAME"] => string(0)"" [" bdate"] => string(0)"" ["接触"] => string(0)"" [" email_add"] => string(0)"" ["可填充":保护] => array(6){[0] => string(5)" fname" [1] => string(5)" mname" [2] => string(5)" lname" [3] => string(5)" bdate" [4] =>字符串(7)"联系" [5] => string(9)" email_add" } [" connection":protected] => NULL [" table":protected] => NULL [" primaryKey":protected] => string(2)" id" ["关键字类型":保护] => string(3)" int" [" perPage":保护] => int(15)["递增"] => bool(true)[" timestamps"] => bool(true)[" attributes":protected] => array(10){[" user_id"] => int(33)[" fname"] => string(6)" Robert" [" L-NAME"] => string(6)" Marley" [" MNAME"] => string(5)" Nesta" [" bdate"] => string(10)" 0000-00-00" ["接触"] => string(11)" 09123847599" [" email_add"] => string(40)" 7c9ac9ca8543dd84f71aa541d422e2a95d3e7450" ["相片"] => NULL [" created_at"] => string(19)" 2017-01-25 01:03:06" ["的updated_at"] => string(19)" 0000-00-00 00:00:00" } [" original":protected] => array(10){[" user_id"] => int(33)[" fname"] => string(6)" Robert" [" L-NAME"] => string(6)" Marley" [" MNAME"] => string(5)" Nesta" [" bdate"] => string(10)" 0000-00-00" ["接触"] => string(11)" 09123847599" [" email_add"] => string(40)" 7c9ac9ca8543dd84f71aa541d422e2a95d3e7450" ["相片"] => NULL [" created_at"] => string(19)" 2017-01-25 01:03:06" ["的updated_at"] => string(19)" 0000-00-00 00:00:00" } [" relations":protected] => array(0){} [" hidden":protected] => array(0){} [" visible":protected] => array(0){} ["追加":protected] => array(0){} [" guarded":protected] => array(1){[0] => string(1)" *" } ["日期":受保护] => array(0){} [" dateFormat":protected] => NULL [" casts":protected] => array(0){} [" touches":protected] => array(0){} [" observables":protected] => array(0){} [" with":protected] => array(0){} [" exists"] => bool(true)[" wasRecentlyCreated"] => bool(false)}

这里当我在$ result上使用foreach到var_dump每个$ user时会发生什么

int(0) string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" bool(true) bool(true) bool(true) bool(false) 

我一直在寻找解决方案,但我似乎找不到一个,我最初使用的是get(),但我读到某个地方,如果我只期待1个结果,我应该先使用(),但它不会改变任何东西。我无法访问Collection ..

1 个答案:

答案 0 :(得分:0)

我找到了解决方案

one帮助了我。

在我的 - > first()上添加 - > toArray是神奇的,它只返回一个具有确切属性的数组而没有不必要的对象属性。