2016-05-08 8 views
0

パブリックフォルダに複数のファイルをアップロードし、データベース内の複数のファイルパスをアップロードしたかったので、パブリックフォルダに複数のファイルをアップロードできましたが、複数のファイルパスをdbに保存できません。 1つのファイル名とパスのみがデータベースに格納されます。ビューの複数のファイルをlaravelのデータベースにアップロードする方法を教えてください。

:コントローラで

<form class="form-horizontal row-border" action="<?= URL::to('/checkiou') ?>" method="post" enctype="multipart/form-data"> 
<input type="hidden" name="_token" value="{{ csrf_token() }}"> 
<input type="file" name="attachments[]" multiple/> 
<input name="save" type="submit" value="Save"> 

public function upload() { 

     // Request the file input named 'attachments' 

    // Request the file input named 'attachments' 
     $files = Request::file('attachments'); 

     //If the array is not empty 
     if ($files[0] != '') { 
     foreach($files as $file) { 
    // Set the destination path 
     $destinationPath = 'uploads'; 
    // Get the orginal filname or create the filename of your choice 
     $filename = $file->getClientOriginalName(); 
    // Copy the file in our upload folder 
     $file->move($destinationPath, $filename); 
    } 

$data1 = DB::table('tbl_iou')->max('iou_id'); 
     $check=0; 
     $check=DB::table('tbl_iou') 
       ->where('iou_id',$data1) 
      ->update(array('image_path'=>$destinationPath, 'file_name'=>$filename)); 

     if ($check > 0) 
     { 
      $_SESSION['msg']="Petty cash details saved Successfully"; 
      return Redirect::to('iou_accounts'); 
     } 
    } 

答えて

0

あなたは、foreachループの外で、データベースにデータを挿入するコードの第二の部分を、実行まず第一に。したがって、最後のファイルに対して1回だけ更新します。

第2に、$data1変数ストアはtbl_iouテーブルから最大(最後になると仮定)のiou_idテーブルのみです。そして、コード内で:

$check=DB::table('tbl_iou') 
    ->where('iou_id',$data1) 
    ->update(array(
     'image_path'=>$destinationPath, 
     'file_name'=>$filename 
    )); 

あなたはこのiou_idが最後にequealある行を検索し、あなたがテーブルの最大iou_idで、一つだけの行の値を更新します。

私はこれを解決するために次の方法を提案します。私はあなたのデータベーステーブルにはiou_idが自動生成されていて、新しい行のみを挿入し、更新はしないと仮定します。

public function upload() { 
     // Request the file input named 'attachments' 

     // Request the file input named 'attachments' 
     $files = Request::file('attachments'); 
     //If the array is not empty 
     if (!empty($files[0])) { 
      $filesData = array(); 
      foreach($files as $file) { 
       // Set the destination path 
       $destinationPath = 'testuploads'; 
       // Get the orginal filname or create the filename of your choice 
       $filename = $file->getClientOriginalName(); 
       // Copy the file in our upload folder 
       $file->move($destinationPath, $filename); 

       // ADD DATA TO TEMPORARY ARRAY 
       $filesData[] = array('image_path'=>$destinationPath, 'file_name'=>$filename); 
      } 

      if(DB::table('tbl_iou')->insert($filesData)){ 
       $_SESSION['msg']="Petty cash details saved Successfully"; 
       return Redirect::to('iou_accounts'); 
      } 
     } 
    } 
関連する問題