2016-11-22 3 views
0

私はPDFを使ってテーブルをエクスポートしようとしていますが、foreachを持っていてforeachからすべてのデータをエクスポートしたいのですが、すべてのデータをエクスポートするだけです。ここでPDFをLaravelからエクスポート

コードです:foreachの内部

foreach ($posts as $post) { 
    $html = '<div class="table-scrollable"> 
        <table id="posts" class="table table-bordered table-hover"> 
         <thead> 
          <tr> 
           <th>Id</th> 
           <th>Name</th> 
           <th>Title</th> 
          </tr> 
         </thead> 
         <tbody id="body"><tr> 
          <td>' 
      . $post->id . ' 
          </td> 
          <td>' . 
      $post->name . 
      '</td> 
          <td>' 
      . $post->title . 
      '</td> 
       </tr> 
       </tbody> 
      </table> 
     </div>'; 
} 
return PDF::load($html, 'A4', 'portrait')->download('my_pdf'); 
+4

を使用して文字列の最後に、あなたのhtmlを追加する必要がありますすべての反復で '$ html'を上書きします。あなたはおそらく、それに追加したいでしょう。 –

+0

はい。しかし、私はどのように知りません。 – Sulde1985

+0

http://php.net/manual/en/language.operators.string.phpにある文字列演算子について読むことがあります –

答えて

0

あなた$html変数を上書き構築、あなたはforeach$htmlを初期化し、連結の割り当て.=

$html = ''; 
foreach ($posts as $post) { 
     $html .= '<div class="table-scrollable"> 
         <table id="posts" class="table table-bordered table-hover"> 
          <thead> 
           <tr> 
            <th>Id</th> 
            <th>Name</th> 
            <th>Title</th> 
           </tr> 
          </thead> 
          <tbody id="body"><tr> 
           <td>' 
       . $post->id . ' 
           </td> 
           <td>' . 
       $post->name . 
       '</td> 
           <td>' 
       . $post->title . 
       '</td> 
        </tr> 
        </tbody> 
       </table> 
      </div>'; 
} 
return PDF::load($html, 'A4', 'portrait')->download('my_pdf'); 
+0

答えてくれてありがとうございますが、これは私がやっていることですが、このエラーが出ています "localhostページが機能していません localhostは現在このリクエストを処理できません。 – Sulde1985

+0

私はあまりにも多くのデータを持っています、それは問題でしたが、解決策は良いです。すべてのおかげで助けになりました。 – Sulde1985

関連する問題