0

我在这个项目中使用laravel-5.4和“maatwebsite/excel”

我打算为我的网站创建一个功能,我可以在其中上传一个 excel 文件,然后在我的视图中显示这个文件的数据

我用来上传数据并将数据发送到后端的表单

public function bundleaddstudent(Request $request)
{

   if($request->hasFile('excelstudent')){

          $File = $request->file('excelstudent');
              $path = $File->getRealPath();

      $data = Excel::load($path, function($reader) {

        })->toObject();


          return view('User.content.tobe_added_students')
          ->with('selection', $this->selection())
          ->with('Students', $data);

    }
  else
  {
    return view('User.content.Add_student')
        ->with('selection', $this->selection())
        ->with('Students', 'No Data')
        ->with('request', $request);
  }
}

现在,已上传并在我的控制器中处理的文件将其转换为数据对象,并且还与新视图一起传递。

我现在需要在我的视图中处理传递的数据,这就是我所做的

 @isset($Students)
    @foreach ($Students as $key => $value)
      <tr>
          <td> {{$value}}</td>
      </tr>
    @endforeach
 @endisset

但可悲的是,这个输出是这样的

在此处输入图像描述

如果我尝试{{ $value->dump() }}

会发生错误,说方法转储不存在。

如果我尝试 {{ $key }}

输出是一个从 0 - 4 开始的整数输出它就像

----------------------------
| studentID  | Firstname  |
----------------------------
|     1      |            |
|     2      |            |
|     3      |            |
|     4      |            |

你明白了

我也尝试过{{ $value->studentId }},但它什么也没显示

在此处输入图像描述

基于我应该尝试的@LrdArc 的评论

@isset( $Students )
      {{  dd( $Students )  }}
@endisset

输出:

LaravelExcelReader {#353 ▼
  +excel: PHPExcel {#361 ▶}
  +reader: PHPExcel_Reader_Excel2007 {#354 ▶}
  +file: "/tmp/phpu3zVBH"
  +columns: []
  +title: "phpu3zVBH"
  +ext: ""
  +encoding: false
  +format: "Excel2007"
  +parsed: RowCollection {#430 ▼
     #title: "Sheet1"
     #items: array:5 [▼
       0 => CellCollection {#495 ▼
          #title: null
          #items: array:9 [▼
             "studentid" => "15-2000332"
             "firstname" => "Dummy1"
             "lastname" => "Dummy1"
             "middlename" => "Dummy1"
             "course" => "Dummy1"
             "ay" => "Dummy1"
             "gender" => "Dummy1"
             "address" => "Dummy1"
             "contact" => "Dummy1"
        ]
      }
      1 => CellCollection {#496 ▼
          #title: null
          #items: array:9 [▼
           "studentid" => "15-2000333"
           "firstname" => "Dummy2"
           "lastname" => "Dummy2"
           "middlename" => "Dummy2"
           "course" => "Dummy2"
           "ay" => "Dummy2"
           "gender" => "Dummy2"
           "address" => "Dummy2"
           "contact" => "Dummy2"
        ]
       }
         2 => CellCollection {#515 ▶}
         3 => CellCollection {#514 ▶}
         4 => CellCollection {#513 ▶}
     ]
     }
4

1 回答 1

1

编辑:好的,我刚刚测试过。在控制器中将 toObject() 更改为 get()。

它是一个对象。在您看来尝试这样的事情:

@isset( $Students )
  @foreach ( $Students as $student )
  <tr>
      <td>{{ $student->studentID }}</td>
      <td>{{ $student->firstname }}</td>
      <td>{{ $student->lastname }}</td>
      <td>{{ $student->middlename }}</td>
  </tr>
  @endforeach
@endisset
于 2017-05-05T16:44:06.033 回答