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

ldioqlga  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(337)

我想把我的图片保存在数据库mysql上,mysql使用一个数组,虽然我可以插入一些东西,但它不是一个图片,而是一个空图片。这是我的视图代码
p、 我这里有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>
rnmwe5a2

rnmwe5a21#

$('#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);

在ajax调用中将formdata作为数据传递
以下更改
你的观点

{!! 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();  
               }  
          });  
     });

相关问题