当我试图在我的Laravel应用程序中打印数组的值时,我得到了一个错误。请看一看。
我的控制器:
public function index()
{
//
$data = new Userflow();
$result = $data->gobalsCount();
$res = $data->yrLogins;
$res1 = $data->mnLogins;
$res2 = $data->dayLogins;
$res3 = $data->hrLogins;
$res4 = $data->minLogins;
$res5 = $data->secLogins;
$object = [$res,$res1,$res2,$res3,$res4,$res5];
//dd($object);
return view('userflow',compact('object'));
}
我的模型:
public $yrLogins = "";
public function gobalsCount(){
$this->yrLogins = DB::SELECT("SELECT count(*)/(TIMESTAMPDIFF(YEAR,'2016-01-01 00:00:00',now())+1) as avg from authtracker where created_at >= '2016-01-01 00:00:00'");
................
我的刀片模板:
@foreach($object as $value)
<td>{{ $value['0']->avg }}</td>
@endforeach
就这样我得到了这种类型的错误。如果是var_dump($object)
,则返回:
array:6 [▼
0 => array:1 [▼
0 => {#1396 ▼
+"avg": "2.0000"
}
]
1 => array:1 [▶]
2 => array:1 [▶]
3 => array:1 [▶]
4 => array:1 [▶]
5 => array:1 [▶]
]
如何打印array
值ex: 2.0000
。任何帮助都将不胜感激。
可以这样试试,
@foreach($object as $value)
<td>{{$value[0][0]->avg}}</td>
@endforeach
还没有试过,但应该可以。