0

I'm trying to display download links(pdf) on my page but I'm getting an index error

Undefined offset: 6 (View: C:\xampp\htdocs\bluepenlabs\fta\app\Modules\Fta\views\frontoffice\Fta\voir-docs.blade.php)

This is my controller :

 public function docs(){
        $docs = Document::all();
        return view("Fta::frontoffice.Fta.voir-docs",compact('docs'));
 }

this is the view

 <?php
       if(file_exists(public_path('frontoffice/fichiers/docs'))){
         $files = File::allFiles(public_path('/frontoffice/fichiers/docs/'));
       }
       if(!empty($docs)){
               foreach ($docs as $doc){
                     //$fichier = explode("/", $file);
                     $fichier = explode("-", $doc['file']);
                    $ext = explode(".", $fichier[6]);

  ?>
        <span style="font-size: x-large;color: royalblue;">{{$doc->titre}}</span>
         <br><br>
         <p style="color: black">{!!  $doc->description !!}</p>
         <a href="/frontoffice/fichiers/docs/{{$doc->id}}/{{$doc->file}}" download style="font-size: x-large">{{$fichier[6]}}</a>

          @if($ext[1]== "pdf")
                <div class="box"><iframe src="/frontoffice/fichiers/docs/{{$doc->id}}/{{$doc->file}}" width = "300px" height = "300px"></iframe></div>
           @endif  
           <br>   
           <medium> Crée le : {{date("Y-m-d", strtotime($doc->created_at))}} <br>Modifier le : {{date("Y-m-d", strtotime($doc->updated_at))}}</medium>
            <hr>
             <?php }
                   }
                  ?>

I tried to dd($fichier) it gives me this :

enter image description here

Atef Rihane
  • 183
  • 2
  • 12
  • Check that `$fichier[6]` actually exists. It obviously does not at least in some situations – RiggsFolly Oct 02 '18 at 09:17
  • `$fichier[6]` might not exist in ALL of your `$docs`. Instead of `dd($fichier)`, which stops your `for` loop after the first element, print out `$fichier[6]` in your blade. – brombeer Oct 02 '18 at 09:18
  • @kerbholz : it displays "BORDEREAU.pdf"... – Atef Rihane Oct 02 '18 at 09:20
  • For all of your `$docs`? Don't know if an edit screwed up your code, but are you sure your `if(isset($docs)){` and `foreach ($docs as $doc){` should end where they do? Shouldn't they end somewhere after ``?? Check your brackets. – brombeer Oct 02 '18 at 09:25
  • @kerbholz : yes it's an edit issue , I removed the extra brackets.. the index error keeps occuring anyway.. – Atef Rihane Oct 02 '18 at 09:31
  • I finally fixed it by simply adding if (array_key_exists(6, $fichier)) before $ext = explode(".", $fichier[6]); – Atef Rihane Oct 02 '18 at 09:46

0 Answers0