【发布时间】:2017-04-05 17:09:18
【问题描述】:
我是 laravel 新手,我试图从 here 解决问题
我有如下控制器:
foreach($users as $user){
$message[] = Model::where('id',$user->id)->get();
}
$data['tests'] = $message;
return View::make('user.index', $data);
我的观点是:
@foreach($tests['message'] as $test)
id : {{$test->id}}
@endforeach
这给了我Undefined index: message
我已经在控制器中转储了$data。我的数组如下所示。我在返回语句之前将 var_dump 放在控制器中。我的var_dump($data) 正在显示:
array(1) {
["tests"] => array(2) {
[0] => object(Illuminate\ Database\ Eloquent\ Collection) #515 (1) { ["items":protected]= > array(2) {
[0] => ["attributes": protected] => array(14) {
["id"] => string(3) "12"....
}
}
[1] => object(Illuminate\ Database\ Eloquent\ Collection) #515 (1) { ["items":protected]= > array(2) {
[0] => ["attributes": protected] => array(14) {
["id"] => string(3) "12"....
}
}
}
我做错了什么。请帮帮我
【问题讨论】:
-
你能展示你的模型结构吗?这取决于您的型号。
-
只包含表名
-
那么,您的消息来自哪里?如果您的数组中不存在密钥(消息),您希望如何使用此密钥?
-
它显然来自模型表
-
是的,没错。根据您的代码,我没有在您的数组中看到消息键。
标签: php arrays laravel foreach