【发布时间】:2016-02-08 18:33:24
【问题描述】:
我正在尝试将一组对象传递给视图,然后使用刀片模板引擎循环遍历它们。
数据是从 Parse.com 查询返回的,其结构如下:
[
{
"Params": [],
"difficulty": "Medium",
"exerciseDescription": "Sit on a gym ball with a dumbbell in each hand. Bend your elbows to lift the dumbbells to your shoulder.",
"exerciseID": "1024",
"exerciseName": "Bicep Curl Sitting on Gym Ball",
"images": [
2758,
2759,
2760
],
"objectId": "9xjQ4WVo6e",
"tags": [
"Dumbbell",
"Gym Ball",
"Flexion"
],
"words": [
"seated",
"dumbbell",
"arm",
"curl"
]
}
]
我使用这个查询得到这个:
public function about()
{
$programmeId = 'T8iqZhtDqe';
$query = new ParseQuery("PrescribedProgrammes");
try {
$programme = $query->get($programmeId);
// The object was retrieved successfully.
} catch (ParseException $ex) {
echo $ex;
// The object was not retrieved successfully.
// error is a ParseException with an error code and message.
}
$exerciseData = $programme->get("exerciseData");
$programmeTitle = $programme->get("prescribedProgrammeTitle");
// return view('pages.about', compact('exerciseData','programmeTitle'));
return view('pages.about')->with('exerciseData', $exerciseData);
}
为了测试这一点,我一直在尝试:
@foreach($exerciseData as $exercise => $value)
{{ $exercise->exerciseName }}
@endforeach
但是我收到Array to string conversion 错误。来自 angularJS 背景,我希望将我的对象数组传递给视图,然后在我认为合适的时候循环遍历它们。
这会被认为是糟糕的形式吗?
编辑
运行dd($exerciseData)
array:7 [▼
0 => array:9 [▼
"Params" => []
"difficulty" => "Medium"
"exerciseDescription" => "Sit on a gym ball with a dumbbell in each hand. Bend your elbows to lift the dumbbells to your shoulder."
"exerciseID" => "1024"
"exerciseName" => "Bicep Curl Sitting on Gym Ball"
"images" => array:6 [▶]
"objectId" => "9xjQ4WVo6e"
"tags" => array:6 [▶]
"words" => array:8 [▶]
]
1 => array:9 [▶]
2 => array:9 [▶]
3 => array:9 [▶]
4 => array:9 [▶]
5 => array:9 [▶]
6 => array:9 [▶]
]
【问题讨论】:
-
不,我不会认为这种糟糕的形式。从程序架构的角度来看,您可能会考虑将循环内脏放在 partial 中,以便循环的业务与显示循环项目的业务保持分离。