2016-03-16 180 views
4

我想表明我的.blade.php數組,但讓我的控制器看起來像這樣它不能正常工作:Laravel打印陣列

class WatchController extends Controller 
{ 

    public function index() 
    { 
     $watchFolderPath = 'C:\\xampp\\htdocs\\Pro\\rec\\'; 
     $watchFolder  = $this->dirToArray($watchFolderPath); 
     return view('watch.new')->with('watchFolder', $watchFolder); 
    } 

    # Get Directories of Path as Array 
    function dirToArray($dir) { 

     $result = array(); 

     $cdir = scandir($dir); 

     foreach ($cdir as $key => $value) 
     { 
      if (!in_array($value,array(".",".."))) 
      { 
       if (is_dir($dir . DIRECTORY_SEPARATOR . $value)) 
       { 
        $result[$value] = $this->dirToArray($dir . DIRECTORY_SEPARATOR . $value); 
       } 
       else 
       { 
        $result[] = $value; 
       } 
      } 
     } 
     return $result; 
    } 
} 

而我的刀我只是試圖內這樣稱呼它:

{{ $watchFolder }} 

,但它沒有工作,我得到以下錯誤:

htmlentities() expects parameter 1 to be string, array given

編輯: 我得到的數組顯示目錄中的子文件夾的所有文件夾/文件。 (使用DD())

目前,它看起來像這樣:

array:6 [▼ 
    123123 => array:2 [▼ 
    "subfolder1" => array:1 [▼ 
     0 => "video.mpg" 
    ] 
    "subfolder2" => array:1 [▶] 
    ] 
    789 => array:2 [▶] 
    "folder1" => array:2 [▶] 
    "folder2" => array:2 [▶] 
    "folder3" => array:2 [▶] 
    "folder1" => [] 
] 
+0

您需要遍歷項目,比如'@ foreach'只需閱讀刀片手冊 – Ghost

+0

要麼是{{print_r($ array,true)}}或'{!! dd($ array)!!}'會幫助你:) – ash

+0

我想稍後在我的刀片上展示它.. –

回答

0

你應該使用@foreach

@foreach ($watchFolder as $key => $value) 
    Key: {{ $key }}  
    Value: {{ $value }} 
@endforeach 

或者

@foreach ($watchFolder as $w) 
    {{ $w->name }}  
@endforeach 
+0

如何打印出數組的所有值? –

+0

如果你問如何打印所有的調試值,你可以嘗試在你的控制器中使用'dd($ watchFolder);'或者{{dd($ watchFolder); }}在你看來。如果你問如何在profuction中輸出數組值,請查看我答案中的代碼。 –

+0

不起作用我得到以下錯誤:在helpers.php中的ErrorException 469行: htmlentities()期望參數1是字符串,給出的數組 –

2

從你的問題就出現如果你想輸出數組用於調試目的,因爲我已經評論過,您可以在刀片模板中簡單使用<?php?>標籤。

<?php dd($array); ?> 

然而,葉片具有原始輸出標記對是{!!!!}

一旦你準備好顯示你的目錄結構的輸出,你應該把它分配給你的視圖。

有兩種使用它的快速方法:使用Reflection;

class WatchController extends Controller 
{ 
    public function index(\Illuminate\Filesystem\Filesystem $filesystem) 
    { 
     $files = $filesystem->allFiles($watchFolderPath); 

     // Your code. 

     return view('name', ['files' => $files]); 
    } 
} 

或者將其從container電話預訂:你的控制器內

class WatchController extends Controller 
{ 
    public function index() 
    { 
     $files = app('files')->allFiles($watchFolderPath); 

     // Your code. 

     return view('name', ['files' => $files]); 
    } 
} 

你也應該是使用Filesystem,就沒有必要重新發明輪子 - 你應該read the manual

0

爲了只輸出在葉片陣列用於調試目的:

 @php 
      var_dump($arr); 
     @endphp 

然後使用foreach循環如上所述。