我知道这很简单..但我觉得我错过了什么,我得到了这个错误,我在发布之前尝试了一切
Trying to get property of non-object
控制器
$temp = Servers::all();
$data = array();
foreach ($temp as $key) {
$data[] = array(
'srv_id' => $key->srv_id,
'srv_name' => $key->srv_name,
'srv_status' => $key->srv_status,
'srv_files' => $key->srv_files,
'srv_disk' => $this->ConvertBytes( $key->srv_disk ),
'srv_ip' => $key->srv_ip
);
}
return View::make( 'pages.server', compact( 'data' ) );
如果我对代码进行vardump,这就是我得到的
的var_dump
array(10) {
[0]=>
array(6) {
["srv_id"]=>
string(3) "389"
["srv_name"]=>
string(3) "u02"
["srv_status"]=>
string(2) "ON"
["srv_files"]=>
string(5) "43832"
["srv_disk"]=>
string(9) "520.07 Gb"
["srv_ip"]=>
string(13) "89.39.196.138"
}
[1]=>
array(6) {
["srv_id"]=>
string(3) "392"
["srv_name"]=>
string(3) "u07"
["srv_status"]=>
string(2) "ON"
["srv_files"]=>
string(5) "42566"
["srv_disk"]=>
string(9) "495.16 Gb"
["srv_ip"]=>
string(13) "89.39.196.147"
}
查看 这就是我在页面中查看数组的方式
@foreach($data as $server)
<td>{{ $server['srv_id'] }}</td>
<td>{{ $server['srv_name'] }}</td>
答案 0 :(得分:0)
您收到Trying to get property of non-object
错误消息的原因是$temp
中的数据是一个数组,并且您尝试将其作为对象进行访问。试试这个。
$temp = Servers::all();
$data = array();
foreach ($temp as $key) {
$data[] = array(
'srv_id' => $key['srv_id'],
'srv_name' => $key['srv_name'],
'srv_status' => $key['srv_status'],
'srv_files' => $key['srv_files'],
'srv_disk' => $this->ConvertBytes( $key['srv_disk'] ),
'srv_ip' => $key['srv_ip']
);
}
可以像这样访问数组:
$foo['bar'];
虽然可以像这样访问对象:
$foo->bar;