Laravel - Popular a list with files in a directory

Asked

Viewed 922 times

-2

I would like to popular a list on a Blade page in the Laravel Framework. I tried to do

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

The $arrayRemessa variable 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 makes:

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 error below:

htmlspecialchars() expects Parameter 1 to be string, array Given


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

Na View, simply:

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

In the 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

On Route: Route::get('itauRefreshAquivosRemessa','Despesascontroller@listararvosremessaitau');

In the method:

public function listarArquivosRemessaItau(){
    $arrayRemessa = array_map('pathinfo', \File::files('assets/remessa'));
    return $arrayRemessa;
}//listarArquivosRemessaItau
  • Where did that class come from File?

  • You can improve your code even more by simply doing $arrayRemessa = array_map('pathinfo', \File::file('assets/remessa')). It is not necessary to do all these operations when using functions that simplify the work

  • Besides, it would be better to use foreach instead of for

  • none of these approaches worked out for me, but I resolved via jQuery.

1 answer

4

That’s because by putting something between {{ }}, Laravel will try to make an escape from whatever is there and only possible escape strings.

In this case, the content of $arrayRemessa[$i] is a array and so is bursting this mistake.

Understand that the return of path_info is a array associative, that is to say, each position in $arrayRemessa contains an associative array with the data from that folder.

As Keys of this array sane dirname, basename, extension and filename.

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

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

Also, the code might 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 the view can stay like this:

@foreach($arrayRemessa as $item)
    <option>{{ $item }}</option>
@endfor
  • I used your code, but select in the View is not populated. It is empty, although the PHP response has captured values. I will make another approach, so that the return of the method that generates the simple array, fills the select via jQuery.

  • @Maurosimoes But what was passed to the view is a simple array. What’s the problem with my answer approach?

  • To tell you the truth, I couldn’t reproduce. I inspected what came out of the method and was populated, but in the View, using either of the two suggestions (with <option>{item}}</option> and the other with <option>{{ $arrayRemessa[$i]['filename'] }}</option>) did not. I must have made some obvious mistake. I thank you very much.

  • @Maurosimoes If you make one dd($arrayView);, which is the way out?

  • The following comes out: array:5 [ 0 => array:4 [ "dirname" => "Assets/shipment" "basename" => "163RC.XLS" "Extension" => "XLS" "filename" => "163RC" ] 1 => array:4 [ "dirname" => "Assets/shipment" "basename" => "163shipment.txt" "Extension" => "txt" "filename" => "163shipment" ] 2 => array:4 [ "dirname" => "Assets/shipment" "basename" => "229RC.COB" ....

  • But this with the following instruction: $arrayRemessa = array_map('pathinfo', File::files('Assets/shipment')); (I did not use Pluck)

  • @Maurosimoes You have to use Pluck, or else specify a key in the view '-'

  • I used it too, but it didn’t roll.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.