Laravel - Popular a list with files in a directory

-2

I would like to populate a list on a Blade page, in the Laravel Framework. I tried

<select id="idSelMinhaLista">
    @if(isset($arrayRemessa))
        @for($i=0;$i < count($arrayRemessa);$i++)
            <option>{{$arrayRemessa[$i]}}</option>
        @endfor
    @endif
</select>

The variable $ arrayRemessa is fed by a call to a PHP method, which, in turn, makes a Return by calling the View Blade that shows the select above. The method does:

public function listarArquivosRemessa(){

    $arrayRemessa = [];
    $filesInFolder = \File::files('assets/remessa');

    foreach($filesInFolder as $path)
    {
        $arrayRemessa[] = pathinfo($path);
    }
    return view ("PesquisaView")->with("arrayRemessa",$arrayRemessa);
}//listarArquivosRemessa

My problem is that I have the following error:

  

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

My 'cowardly' exit was to use jQuery. This is:

In the View, simply:

<select id="idSelItauArquivosRemessa">
</select>
<button type="button" id="idBtnRefreshListaRemessaItau">Refresh</button>

No jQuery:

jQuery("#idBtnRefreshListaRemessa").click(function (){
    var objSel=jQuery("#idSelItauArquivosRemessa");
    objSel.empty();
    var concatena='<option value="0" title="0"></option>';
        jQuery.get("itauRefreshArquivosRemessa",function(retorno){
            for(i=0;i < retorno.length;i++){
                concatena=concatena+'<option value="'+retorno[i].basename+' "title="'+retorno[i].basename+'">'+retorno[i].basename+'</option>';
            }//for
         objSel.append(concatena);
        });//get
});//idBtnRefreshListaRemessa

Na Route:   Route :: get ('itauRefreshArchivesRemessa', 'ExpensesController @ listArchivesRemessaItau');

In method:

public function listarArquivosRemessaItau(){
    $arrayRemessa = array_map('pathinfo', \File::files('assets/remessa'));
    return $arrayRemessa;
}//listarArquivosRemessaItau
    
asked by anonymous 14.11.2017 / 13:09

1 answer

5

This is because when you put something between {{ }} , Laravel will try to escape what is there and only escape strings .

In this case, the content of $arrayRemessa[$i] is an array and so it is popping this error.

Understand that the return of path_info is an associative array, that is, each position in $arrayRemessa contains an associative array with the data in that folder. / p>

The keys are dirname , basename , extension and filename .

You will need to choose which one you want to show in view , for example:

@for($i=0;$i < count($arrayRemessa);$i++)
    <option>{{ $arrayRemessa[$i]['filename'] }}</option>
@endfor

In addition, the code can get a little better:

public function listarArquivosRemessa()
{    
    $arrayRemessa = array_map('pathinfo', \File::files('assets/remessa'));
    $arrayView = array_pluck($arrayRemessa, 'dirname');
    // (^) Isso vai extrair apenas as keys que você precisa

    return view ("PesquisaView")->with("arrayRemessa", $arrayView);
}

And in view it looks like this:

@foreach($arrayRemessa as $item)
    <option>{{ $item }}</option>
@endfor
    
14.11.2017 / 13:13