Laravel-在数据库中保存或插入图像数组-Laravel

时间:2018-11-19 05:32:53

标签: mysql arrays ajax laravel

我想将图像保存在使用数组的mysql数据库上,我可以插入一些东西,但它不是图像,而是空图像。这是我的视图代码

P.S我这里有一个ajax,这就是为什么它有一个ID

  

这是我的视图

  {!! Form::open(['action'=>'Admin\PromotionsController@store', 'method' => 'POST','enctype'=>'multipart/form-data']) !!}
<div class="form-group">  
     <form name="add_name" id="add_name">  
          <div class="table-responsive">  
               <table class="table table-bordered" id="dynamic_field">  
                    <tr>  
                         <td> {{Form::file('promotion_image[]')}}</td>

                         <td>{{ Form::button('Add', ['class' => 'btn btn-success', 'id'=>'add','name'=>'add']) }}</td>
                    </tr>  
               </table>  
               {{Form::submit('submit', ['class'=>'btn btn-primary'])}}
          </div>  
     </form>  
</div>  
{!! Form::close() !!}
  

这是我的控制器,将存储或保存图像

 $this->validate($request, [
        'promotion_image' => 'required'
    ]);

   //Handle File Upload
   if($request->hasFile('promotion_image[]')){
    // Get FileName
    $filenameWithExt = $request->file('promotion_image[]')->getClientOriginalName();
    //Get just filename
    $filename = pathinfo( $filenameWithExt, PATHINFO_FILENAME);
    //Get just extension
    $extension = $request->file('promotion_image[]')->getClientOriginalExtension();
    //Filename to Store
    $fileNameToStore = $filename.'_'.time().'.'.$extension;
    //Upload Image
    $path = $request->file('promotion_image[]')->storeAs('public/promotion_images',$fileNameToStore);
    }else{
        $fileNameToStore='noimage.jpg';
    }

    $promotion = new Promotion;
    $promotion->promotion_image = $fileNameToStore;
    $promotion->save();
  

我的AJAX代码

<script>  
$(document).ready(function(){  
     var i=1;  
     $('#add').click(function(){  
          i++;  
          $('#dynamic_field').append('<tr id="row'+i+'"><td>{{Form::file('promotion_image[]',['class'=>'form-control name_list'])}}</td><td><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove">X</button></td></tr>');  
     });  
     $(document).on('click', '.btn_remove', function(){  
          var button_id = $(this).attr("id");   
          $('#row'+button_id+'').remove();  
     });  
     $('#submit').click(function(){            
          $.ajax({  
               url:"name.php",  
               method:"POST",  
               data:$('#add_name').serialize(),  
               success:function(data)  
               {  
                    alert(data);  
                    $('#add_name')[0].reset();  
               }  
          });  
     });  
});  
</script>

1 个答案:

答案 0 :(得分:0)

$('#add_name').serialize() using this you can't get image object in post using ajax.

您应该使用formdata

var form = $('form')[0];
var formData = new FormData(form);

将formData作为数据传递给您的Ajax调用

以下更改

您的视图

{!! Form::open(['action'=>'Admin\PromotionsController@store', 'method' => 'POST','enctype'=>'multipart/form-data', 'name' => 'add_name', 'id' => 'add_name']) !!}
<div class="form-group">   
    <div class="table-responsive">  
        <table class="table table-bordered" id="dynamic_field">  
           <tr>  
              <td> {{Form::file('promotion_image[]')}}</td>

              <td>{{ Form::button('Add', ['class' => 'btn btn-success', 'id'=>'add','name'=>'add']) }}</td>
           </tr>  
        </table>  
        {{Form::submit('submit', ['class'=>'btn btn-primary'])}}
    </div> 
</div>  
{!! Form::close() !!}

对于控制器功能,您应该使用foreach

如果您使用的是最新的laravel版本

$this->validate($request, [
    'promotion_image' => 'required'
]);

if ($request->has('promotion_image'))
{
    $promotion_images = [];
    foreach ($request->file('promotion_image') as $key => $file)
    {
        $image = \Storage::put('promotion_image', $file); // your image path

        if ($image)
            array_push($promotion_images, $image);
    }

    $fileNameToStore = serialize($promotion_images);

}
else
{
    $fileNameToStore='noimage.jpg';
}
$promotion = new Promotion;
$promotion->promotion_image = $fileNameToStore;
$promotion->save();

或者按照您的逻辑

$this->validate($request, [
    'promotion_image' => 'required'
]);

if ($request->has('promotion_image'))
{   
    //Handle File Upload

    $promotion_images = [];
    foreach ($request->file('promotion_image') as $key => $file)
    {
        // Get FileName
        $filenameWithExt = $file->getClientOriginalName();
        //Get just filename
        $filename = pathinfo( $filenameWithExt, PATHINFO_FILENAME);
        //Get just extension
        $extension = $file->getClientOriginalExtension();
        //Filename to Store
        $fileNameToStore = $filename.'_'.time().'.'.$extension;
        //Upload Image
        $path = $file->storeAs('public/promotion_images',$fileNameToStore);
        array_push($promotion_images, $fileNameToStore);
    }

    $fileNameToStore = serialize($promotion_images);
}
else
{
    $fileNameToStore='noimage.jpg';
}

$promotion = new Promotion;
$promotion->promotion_image = $fileNameToStore;
$promotion->save();

并更改您的Ajax脚本代码

更改提交方法

$('#submit').click(function(){  
        var form = $('#add_name')[0];
        var formData = new FormData(form);

          $.ajax({  
               url:"name.php",  
               method:"POST",  
               data:formData,  
               success:function(data)  
               {  
                    alert(data);  
                    $('#add_name')[0].reset();  
               }  
          });  
     });