【问题标题】:getting an Invalid argument supplied for foreach() in laravel 5.4在 laravel 5.4 中获取为 foreach() 提供的无效参数
【发布时间】:2017-07-17 03:34:32
【问题描述】:

我正在尝试访问一个 api,但不幸的是我得到了一个无效的参数。 下面是我在视图中的代码。

<thead>
  <tr>
    <th>Id</th>
    <th>Name</th>
    <th>Address</th>
    <th>Career</th>
  </tr>
</thead>
    <tbody>
      @foreach ($students as $student)
      <tr>
        <td>{{$student->id}}</td>
        <td>{{$student->name}}</td>
        <td>{{$student->address}}</td>
        <td>{{$student->career}}</td>
      </tr>
        @endforeach
    </tbody>

下面是我的学生控制器。

class StudentController extends ClientController
 {
       public function getAllStudents()
  {
    $students = $this->obtainAllStudents();

  return view('students/all-students', ['students' => $students]);
   }
 }

获取allAllStudents函数取自ClientController类

protected function obtainAllStudents()
{
  $this->performGetRequest('https://lumenapi.juandmegon.com/students');


}

【问题讨论】:

  • 请 dd($students) 你的变量,这样你就可以看到你得到了什么。

标签: php laravel-5.4 guzzle


【解决方案1】:

你需要解析的数据应该是这样的

  return view('students/all-students', ['students' => $students->data]);

我能看到您的回复

'https://lumenapi.juandmegon.com/students'

您正在获取包含数组的对象,要遍历数组,您必须首先获取该对象。

或者你可以直接在刀片文件中使用,比如

@foreach ($students->data as $student)

你还没有在函数中返回

protected function obtainAllStudents()
{
//you missed return here
  return $this->performGetRequest('https://lumenapi.juandmegon.com/students');
}

【讨论】:

  • 当我在控制器中添加这个return view('students/all-students', ['students' =&gt; $students-&gt;data]);
  • 我收到Trying to get property of non-object
  • 你需要在受保护的函数getAllStudents()函数中添加return
  • 对不起,我不明白你的意思?
  • 我已经更新了答案请看你错过了退货声明,
猜你喜欢
  • 2018-10-06
  • 1970-01-01
  • 2018-12-09
  • 1970-01-01
  • 2015-06-29
  • 1970-01-01
  • 1970-01-01
  • 2017-02-15
  • 2017-05-03
相关资源
最近更新 更多