Laravel中的未定义变量 - 将数组从控制器传递到视图

时间:2017-08-13 20:48:55

标签: php laravel

我刚刚在Phpstorm中安装了Xdebug,我遇到了各种各样的问题。我正在运行PHP-version 7.1.8和Xdebug version 2.5.5)。我一直在写下面的错误。

关于Undefined variable: csvfile的部分我没有得到......我已将它从我的控制器传递到我的视图 - 我甚至可以显示它并处理该数组中的数据(使用{{ 1}}例如):

来自Xdebug的错误

print_r

我所有的网络路线:

/usr/local/bin/php /Users/[USERNAME]/path_to_project/resources/views/home/minor.blade.php
@extends('layouts.app')
PHP Notice:  Undefined variable: csvfile in /Users/[USERNAME]/path_to_project/resources/views/home/minor.blade.php on line 19

PHP Stack trace:
PHP   1. {main}() /Users/[USERNAME]/path_to_project/resources/views/home/minor.blade.php:0
PHP Warning:  Invalid argument supplied for foreach() in /Users/[USERNAME]/path_to_project/resources/views/home/minor.blade.php on line 19
PHP Stack trace:
PHP   1. {main}() /Users/[USERNAME]/path_to_project/resources/views/home/minor.blade.php:0
@section('title', 'Minor page')

@section('content')
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-lg-12">
                <div class="m-t-lg">
                    <h1>
                        Testarea
                    </h1>


Notice: Undefined variable: csvfile in /Users/[USERNAME]/path_to_project/resources/views/home/minor.blade.php on line 19

Call Stack:
        0.0029     359184   1. {main}() /Users/[USERNAME]/path_to_project/resources/views/home/minor.blade.php:0


Warning: Invalid argument supplied for foreach() in /Users/[USERNAME]/path_to_project/resources/views/home/minor.blade.php on line 19

Call Stack:
        0.0029     359184   1. {main}() /Users/[USERNAME]/path_to_project/resources/views/home/minor.blade.php:0


                </div>
            </div>
        </div>
    </div>
@endsection

Process finished with exit code 0

我的TimeRecordController:

Route::get('/testcsv', 'TimeRecordController@importCsv')->name("test");

我的class TimeRecordController extends Controller { // public function index() { // return view( 'home/minor' ); // } function csvToArray( $filename = '', $delimiter = ',' ) { if ( ! file_exists( $filename ) || ! is_readable( $filename ) ) { return false; } $header = null; $data = array(); if ( ( $handle = fopen( $filename, 'r' ) ) !== false ) { while ( ( $row = fgetcsv( $handle, 10000, $delimiter ) ) !== false ) { if ( ! $header ) { $header = $row; } else { $data[] = array_combine( $header, $row ); } } fclose( $handle ); } return $data; } public function importCsv() { $file = public_path( 'file/test.csv' ); $csvfile = $this->csvToArray( $file ); return view( 'home/minor' , compact('csvfile' ) ); } } - 查看:

home/minor

更新

根据要求,这里是@extends('layouts.app') @section('title', 'Minor page') @section('content') <div class="wrapper wrapper-content animated fadeInRight"> <div class="row"> <div class="col-lg-12"> <div class="m-t-lg"> <h1> Testarea </h1> <?php date_default_timezone_set( 'Europe/Copenhagen' ); xdebug_break(); $counter = 0; foreach($csvfile as $value){ if($counter < 10){ $new_date = explode('.', $value['Date']); $new_date = $new_date['2'] . '-' . $new_date['1'] . '-' . $new_date['0']; $start_datestamp_raw_string = $new_date . 'T' . $value['Check-In']; $end_datestamp_raw_string = $new_date . 'T' . $value['Check-Out']; $start_stamp = new DateTime($start_datestamp_raw_string); $end_stamp = new DateTime($end_datestamp_raw_string); echo $start_stamp->format('H:i') . ' to ' . $end_stamp->format('H:i'); $datetime1 = new DateTime('2009-10-11'); $datetime2 = new DateTime('2009-10-13'); $interval = $start_stamp->diff($end_stamp); echo '<br />'; echo '<br />'; echo 'Diff: ' . $interval->format("%H:%I"); echo '<br />'; echo '<br />'; } $counter += 1; } ?> </div> </div> </div> </div> @endsection 的输出,写在视图中dd( $csvfile ) - 语句的正上方。有172行,所以我刚刚扩展了其中两行:

foreach

0 个答案:

没有答案