【问题标题】:Laravel 5 displaying an Array of Objects returned from Parse.comLaravel 5 显示从 Parse.com 返回的对象数组
【发布时间】: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 中,以便循环的业务与显示循环项目的业务保持分离。

标签: php arrays laravel object


【解决方案1】:

我认为您使用的是数组的键而不是值。例如,默认的 PHP foreach 如下所示:

foreach($array as $key => $value) {
    // code
}

编辑: 查看 $exerciseData 的转储后,似乎 PHP 正在将 JSON 序列化为一个数组,因此这改变了答案。

如果你把它返回到视图中:

return view('quiz.create', compact('programmeTitle', 'exerciseData'));

然后在您看来,它应该可以正常工作,因为我已经在本地进行了测试:

<h2>{{$programmeTitle}}</h2>
@foreach ($exerciseData as $key => $exercise)
    <p>{{$exercise['exerciseName']}}</p>
@endforeach

【讨论】:

  • 我在尝试使用return view('pages.about', compact('exerciseData'));时收到此错误Array to string conversion (View: /Users/myusername/Sites/PHP/mayappl/test-Guru/resources/views/pages/about.blade.php)
  • 你能dd($exerciseData); 让我们看看那个变量的结构吗?
  • 我已将此添加到我的帖子中
  • 好的,你想要实现的结构是什么,我认为它将 $programmeTitle 添加到每个 $excerciseData 对象是对的吗?如果没有额外的 $programmeTitle 东西,它应该可以使用:return view('pages.about')-&gt;with('exerciseData', $exerciseData);@foreach($exerciseData as $key =&gt; $exercise) {{ $exercise-&gt;exerciseName }} @endforeach
  • 对不起,我的意思是对象结构!您需要$programmeTitle 在哪里?
猜你喜欢
  • 1970-01-01
  • 2020-01-05
  • 2016-01-21
  • 2015-04-30
  • 1970-01-01
  • 1970-01-01
  • 2016-08-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多